use of org.teiid.translator.TranslatorException in project teiid by teiid.
the class FileExecutionFactory method createProcedureExecution.
// @Override
public ProcedureExecution createProcedureExecution(final Call command, final ExecutionContext executionContext, final RuntimeMetadata metadata, final Connection conn) throws TranslatorException {
if (conn instanceof VirtualFileConnection) {
return new VirtualFileProcedureExecution(command, (VirtualFileConnection) conn);
}
final FileConnection fc = (FileConnection) conn;
if (command.getProcedureName().equalsIgnoreCase(SAVEFILE)) {
return new ProcedureExecution() {
@Override
public void execute() throws TranslatorException {
String filePath = (String) command.getArguments().get(0).getArgumentValue().getValue();
Object file = command.getArguments().get(1).getArgumentValue().getValue();
if (file == null || filePath == null) {
// $NON-NLS-1$
throw new TranslatorException(UTIL.getString("non_null"));
}
// $NON-NLS-1$
LogManager.logDetail(LogConstants.CTX_CONNECTOR, "Saving", filePath);
InputStream is = null;
try {
if (file instanceof SQLXML) {
is = ((SQLXML) file).getBinaryStream();
} else if (file instanceof Clob) {
is = new ReaderInputStream(((Clob) file).getCharacterStream(), encoding);
} else if (file instanceof Blob) {
is = ((Blob) file).getBinaryStream();
} else {
// $NON-NLS-1$
throw new TranslatorException(UTIL.getString("unknown_type"));
}
ObjectConverterUtil.write(is, fc.getFile(filePath));
} catch (IOException e) {
// $NON-NLS-1$
throw new TranslatorException(e, UTIL.getString("error_writing"));
} catch (SQLException e) {
// $NON-NLS-1$
throw new TranslatorException(e, UTIL.getString("error_writing"));
} catch (ResourceException e) {
// $NON-NLS-1$
throw new TranslatorException(e, UTIL.getString("error_writing"));
}
}
@Override
public void close() {
}
@Override
public void cancel() throws TranslatorException {
}
@Override
public List<?> next() throws TranslatorException, DataNotAvailableException {
return null;
}
@Override
public List<?> getOutputParameterValues() throws TranslatorException {
return Collections.emptyList();
}
};
} else if (command.getProcedureName().equalsIgnoreCase(DELETEFILE)) {
return new ProcedureExecution() {
@Override
public void execute() throws TranslatorException {
String filePath = (String) command.getArguments().get(0).getArgumentValue().getValue();
if (filePath == null) {
// $NON-NLS-1$
throw new TranslatorException(UTIL.getString("non_null"));
}
// $NON-NLS-1$
LogManager.logDetail(LogConstants.CTX_CONNECTOR, "Deleting", filePath);
try {
File f = fc.getFile(filePath);
if (!f.exists()) {
if (exceptionIfFileNotFound) {
// $NON-NLS-1$
throw new TranslatorException(DataPlugin.Util.gs("file_not_found", filePath));
}
} else if (!f.delete()) {
// $NON-NLS-1$
throw new TranslatorException(UTIL.getString("error_deleting"));
}
} catch (ResourceException e) {
// $NON-NLS-1$
throw new TranslatorException(e, UTIL.getString("error_deleting"));
}
}
@Override
public void close() {
}
@Override
public void cancel() throws TranslatorException {
}
@Override
public List<?> next() throws TranslatorException, DataNotAvailableException {
return null;
}
@Override
public List<?> getOutputParameterValues() throws TranslatorException {
return Collections.emptyList();
}
};
}
return new FileProcedureExecution(command, fc);
}
use of org.teiid.translator.TranslatorException in project teiid by teiid.
the class LanguageBridgeFactory method translate.
/* Execute */
Call translate(StoredProcedure sp) {
Procedure proc = null;
if (sp.getProcedureID() != null) {
try {
proc = this.metadataFactory.getProcedure(sp.getGroup().getName());
} catch (TranslatorException e) {
throw new TeiidRuntimeException(QueryPlugin.Event.TEIID30486, e);
}
}
Class<?> returnType = null;
List<Argument> translatedParameters = new ArrayList<Argument>();
for (SPParameter param : sp.getParameters()) {
Direction direction = Direction.IN;
switch(param.getParameterType()) {
case ParameterInfo.IN:
direction = Direction.IN;
break;
case ParameterInfo.INOUT:
direction = Direction.INOUT;
break;
case ParameterInfo.OUT:
direction = Direction.OUT;
break;
case ParameterInfo.RESULT_SET:
// already part of the metadata
continue;
case ParameterInfo.RETURN_VALUE:
returnType = param.getClassType();
continue;
}
if (param.isUsingDefault() && BaseColumn.OMIT_DEFAULT.equalsIgnoreCase(metadataFactory.getMetadata().getExtensionProperty(param.getMetadataID(), BaseColumn.DEFAULT_HANDLING, false))) {
continue;
}
ProcedureParameter metadataParam = metadataFactory.getParameter(param);
// we can assume for now that all arguments will be literals, which may be multivalued
org.teiid.language.Expression value = null;
if (direction != Direction.OUT) {
if (param.isVarArg()) {
ArrayImpl av = (ArrayImpl) ((Constant) param.getExpression()).getValue();
if (av != null) {
for (Object obj : av.getValues()) {
Argument arg = new Argument(direction, new Literal(obj, param.getClassType().getComponentType()), param.getClassType().getComponentType(), metadataParam);
translatedParameters.add(arg);
}
}
break;
}
value = translate(param.getExpression());
}
Argument arg = new Argument(direction, value, param.getClassType(), metadataParam);
translatedParameters.add(arg);
}
Call call = new Call(removeSchemaName(sp.getProcedureName()), translatedParameters, proc);
call.setReturnType(returnType);
return call;
}
use of org.teiid.translator.TranslatorException in project teiid by teiid.
the class ConnectorManager method buildCapabilities.
private BasicSourceCapabilities buildCapabilities(ExecutionFactory<Object, Object> translator) throws TranslatorException {
if (translator.isSourceRequiredForCapabilities()) {
Object connection = null;
Object connectionFactory = null;
try {
connectionFactory = getConnectionFactory();
if (connectionFactory != null) {
connection = translator.getConnection(connectionFactory, null);
}
if (connection == null) {
// $NON-NLS-1$);
throw new TranslatorException(QueryPlugin.Event.TEIID31108, QueryPlugin.Util.getString("datasource_not_found", getConnectionName()));
}
if (connection instanceof WrappedConnection) {
try {
connection = ((WrappedConnection) connection).unwrap();
} catch (ResourceException e) {
throw new TranslatorException(QueryPlugin.Event.TEIID30477, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30477, getConnectionName()));
}
}
// $NON-NLS-1$
LogManager.logDetail(LogConstants.CTX_CONNECTOR, "Initializing the capabilities for", translatorName);
synchronized (executionFactory) {
executionFactory.initCapabilities(connection);
}
} finally {
if (connection != null) {
translator.closeConnection(connection, connectionFactory);
}
}
}
BasicSourceCapabilities resultCaps = CapabilitiesConverter.convertCapabilities(translator, id);
return resultCaps;
}
use of org.teiid.translator.TranslatorException in project teiid by teiid.
the class LDAPQueryExecution method convertSingleValue.
private Object convertSingleValue(Column modelElement, String modelAttrName, Class<?> modelAttrClass, Object objResult) throws TranslatorException, InvalidNameException {
if (objResult == null) {
return null;
}
// return null.
if (!(objResult instanceof String)) {
return objResult;
}
String strResult = (String) objResult;
// There is no way to differentiate between being unset and being the empty string.
if (strResult.equals("")) {
// $NON-NLS-1$
return null;
}
// MPW: 3-11-07: Added support for java.lang.Integer conversion.
if (TypeFacility.RUNTIME_TYPES.TIMESTAMP.equals(modelAttrClass)) {
String timestampFormat = modelElement.getFormat();
if (timestampFormat == null) {
timestampFormat = LDAPConnectorConstants.ldapTimestampFormat;
}
SimpleDateFormat dateFormat = new SimpleDateFormat(timestampFormat);
try {
Date dateResult = dateFormat.parse(strResult);
Timestamp tsResult = new Timestamp(dateResult.getTime());
return tsResult;
} catch (ParseException pe) {
// $NON-NLS-1$
throw new TranslatorException(pe, LDAPPlugin.Util.getString("LDAPSyncQueryExecution.timestampParseFailed", modelAttrName));
}
// TODO: Extend support for more types in the future.
// Specifically, add support for byte arrays, since that's actually supported
// in the underlying data source.
}
// extract rdn
String type = modelElement.getProperty(LDAPExecutionFactory.RDN_TYPE, false);
if (type != null) {
String prefix = modelElement.getProperty(LDAPExecutionFactory.DN_PREFIX, false);
LdapName name = new LdapName(strResult);
if (prefix != null) {
if (!name.getPrefix(name.size() - 1).toString().equals(prefix)) {
throw new InvalidNameException();
}
} else if (name.size() > 1) {
throw new InvalidNameException();
}
Rdn rdn = name.getRdn(name.size() - 1);
if (!rdn.getType().equals(type)) {
throw new InvalidNameException();
}
return rdn.getValue();
}
// the Teiid type conversion logic will handle refine from here if necessary
return strResult;
}
use of org.teiid.translator.TranslatorException in project teiid by teiid.
the class LDAPQueryExecution method getValue.
/**
* Add Result to Row
* @param modelElement the model element
* @param attrs the attributes
* @param row the row
* @throws InvalidNameException
*/
// GHH 20080326 - added resultDistinguishedName to method signature. If
// there is an element in the model named "DN" and there is no attribute
// with this name in the search result, we return this new parameter
// value for that column in the result
// GHH 20080326 - added handling of ClassCastException when non-string
// attribute is returned
private Object getValue(Column modelElement, SearchResult result, Attributes attrs, boolean unwrap) throws TranslatorException, InvalidNameException {
String modelAttrName = modelElement.getSourceName();
Class<?> modelAttrClass = modelElement.getJavaType();
String multivalAttr = modelElement.getDefaultValue();
if (modelAttrName == null) {
// $NON-NLS-1$
final String msg = LDAPPlugin.Util.getString("LDAPSyncQueryExecution.nullAttrError");
throw new TranslatorException(msg);
}
Attribute resultAttr = attrs.get(modelAttrName);
// If the attribute is not present, we return NULL.
if (resultAttr == null) {
// if DN attribute is not present in search result
if (modelAttrName.equalsIgnoreCase("DN")) {
// $NON-NLS-1$
return result.getNameInNamespace();
}
return null;
}
Object objResult = null;
try {
if (TypeFacility.RUNTIME_TYPES.STRING.equals(modelAttrClass) && MULTIVALUED_CONCAT.equalsIgnoreCase(multivalAttr)) {
// mpw 5/09
// Order the multi-valued attrs alphabetically before creating a single string,
// using the delimiter to separate each token
ArrayList<String> multivalList = new ArrayList<String>();
NamingEnumeration<?> attrNE = resultAttr.getAll();
int length = 0;
while (attrNE.hasMore()) {
String val = (String) attrNE.next();
multivalList.add(val);
length += ((val == null ? 0 : val.length()) + 1);
}
Collections.sort(multivalList);
StringBuilder multivalSB = new StringBuilder(length);
Iterator<String> itr = multivalList.iterator();
while (itr.hasNext()) {
multivalSB.append(itr.next());
if (itr.hasNext()) {
multivalSB.append(delimiter);
}
}
return multivalSB.toString();
}
if (modelAttrClass.isArray()) {
return getArray(modelAttrClass.getComponentType(), resultAttr, modelElement, modelAttrName);
}
if (unwrap && resultAttr.size() > 1) {
return getArray(modelAttrClass, resultAttr, modelElement, modelAttrName);
}
// just a single value
objResult = resultAttr.get();
} catch (NamingException ne) {
// $NON-NLS-1$m
final String msg = LDAPPlugin.Util.gs(LDAPPlugin.Event.TEIID12004, modelAttrName) + " : " + ne.getExplanation();
LogManager.logWarning(LogConstants.CTX_CONNECTOR, msg);
throw new TranslatorException(ne, msg);
}
return convertSingleValue(modelElement, modelAttrName, modelAttrClass, objResult);
}
Aggregations