use of org.eclipse.core.runtime.IAdapterManager in project eclipse.platform.runtime by eclipse.
the class AdaptExpression method evaluate.
@Override
public EvaluationResult evaluate(IEvaluationContext context) throws CoreException {
if (fTypeName == null)
return EvaluationResult.FALSE;
Object var = context.getDefaultVariable();
if (var == null) {
return EvaluationResult.FALSE;
}
Object adapted = null;
IAdapterManager manager = Platform.getAdapterManager();
if (Expressions.isInstanceOf(var, fTypeName)) {
adapted = var;
} else {
// try to see if the variable itself implements IAdaptable
if (var instanceof IAdaptable) {
Class<?> typeClazz = Expressions.loadClass(var.getClass().getClassLoader(), fTypeName);
if (typeClazz != null) {
adapted = ((IAdaptable) var).getAdapter(typeClazz);
}
}
if (adapted == null) {
if (forceLoadEnabled()) {
adapted = manager.loadAdapter(var, fTypeName);
} else {
adapted = manager.getAdapter(var, fTypeName);
if (adapted == null) {
if (manager.queryAdapter(var, fTypeName) == IAdapterManager.NOT_LOADED) {
return EvaluationResult.NOT_LOADED;
} else {
return EvaluationResult.FALSE;
}
}
}
if (adapted == null) {
// all attempts failed, return false
return EvaluationResult.FALSE;
}
}
}
// if the adapter is loaded.
return evaluateAnd(new DefaultVariable(context, adapted));
}
use of org.eclipse.core.runtime.IAdapterManager in project eclipse.platform.runtime by eclipse.
the class IAdapterManagerServiceTest method testGetAdapter.
/**
* Tests API method IAdapterManager.getAdapter.
*/
public void testGetAdapter() {
IAdapterManager manager = getAdapterManager();
TestAdaptable adaptable = new TestAdaptable();
// request non-existing adaptable
assertNull("1.0", manager.getAdapter("", NON_EXISTING));
// request adapter that is in XML but has no registered factory
Object result = manager.getAdapter(adaptable, TEST_ADAPTER);
assertTrue("1.1", result instanceof TestAdapter);
// request adapter that is not in XML
assertNull("1.2", manager.getAdapter(adaptable, "java.lang.String"));
// register an adapter factory that maps adaptables to strings
IAdapterFactory fac = new IAdapterFactory() {
@Override
public <T> T getAdapter(Object adaptableObject, Class<T> adapterType) {
if (adapterType == String.class) {
return adapterType.cast(adaptableObject.toString());
}
return null;
}
@Override
public Class<?>[] getAdapterList() {
return new Class[] { String.class };
}
};
manager.registerAdapters(fac, TestAdaptable.class);
try {
// request adapter for factory that we've just added
result = manager.getAdapter(adaptable, "java.lang.String");
assertTrue("1.3", result instanceof String);
} finally {
manager.unregisterAdapters(fac, TestAdaptable.class);
}
// request adapter that was unloaded
assertNull("1.4", manager.getAdapter(adaptable, "java.lang.String"));
}
use of org.eclipse.core.runtime.IAdapterManager in project eclipse.platform.runtime by eclipse.
the class IAdapterManagerServiceTest method testLoadAdapter.
/**
* Tests API method IAdapterManager.loadAdapter.
*/
public void testLoadAdapter() {
IAdapterManager manager = getAdapterManager();
TestAdaptable adaptable = new TestAdaptable();
// request non-existing adaptable
assertNull("1.0", manager.loadAdapter("", NON_EXISTING));
// request adapter that is in XML but has no registered factory
Object result = manager.loadAdapter(adaptable, TEST_ADAPTER);
assertTrue("1.1", result instanceof TestAdapter);
// request adapter that is not in XML
assertNull("1.2", manager.loadAdapter(adaptable, "java.lang.String"));
// register an adapter factory that maps adaptables to strings
IAdapterFactory fac = new IAdapterFactory() {
@Override
public <T> T getAdapter(Object adaptableObject, Class<T> adapterType) {
if (adapterType == String.class) {
return adapterType.cast(adaptableObject.toString());
}
return null;
}
@Override
public Class<?>[] getAdapterList() {
return new Class[] { String.class };
}
};
manager.registerAdapters(fac, TestAdaptable.class);
try {
// request adapter for factory that we've just added
result = manager.loadAdapter(adaptable, "java.lang.String");
assertTrue("1.3", result instanceof String);
} finally {
manager.unregisterAdapters(fac, TestAdaptable.class);
}
// request adapter that was unloaded
assertNull("1.4", manager.loadAdapter(adaptable, "java.lang.String"));
}
use of org.eclipse.core.runtime.IAdapterManager in project ecf by eclipse.
the class Activator method getAdapterManager.
public IAdapterManager getAdapterManager() {
// First, try to get the adapter manager via
if (adapterManagerTracker == null) {
adapterManagerTracker = new ServiceTracker(this.context, IAdapterManager.class.getName(), null);
adapterManagerTracker.open();
}
IAdapterManager adapterManager = (IAdapterManager) adapterManagerTracker.getService();
// PlatformHelper class
if (adapterManager == null)
adapterManager = PlatformHelper.getPlatformAdapterManager();
if (adapterManager == null)
// $NON-NLS-1$
getDefault().log(new Status(IStatus.ERROR, PLUGIN_ID, IStatus.ERROR, "Cannot get adapter manager", null));
return adapterManager;
}
use of org.eclipse.core.runtime.IAdapterManager in project ecf by eclipse.
the class XMPPFileTransferRequestListener method fileTransferRequest.
/*
* (non-Javadoc)
*
* @see
* org.jivesoftware.smackx.filetransfer.FileTransferListener#fileTransferRequest
* (org.jivesoftware.smackx.filetransfer.FileTransferRequest)
*/
public void fileTransferRequest(final FileTransferRequest request) {
requestListener.handleFileTransferRequest(new IFileTransferRequestEvent() {
boolean requestAccepted = false;
IFileTransferInfo fileTransferInfo = new IFileTransferInfo() {
Map props = new HashMap();
File f = new File(request.getFileName());
public String getDescription() {
return request.getDescription();
}
public File getFile() {
return f;
}
public Map getProperties() {
return props;
}
public Object getAdapter(Class adapter) {
if (adapter == null)
return null;
if (adapter.isInstance(this))
return this;
final IAdapterManager adapterManager = XmppPlugin.getDefault().getAdapterManager();
return (adapterManager == null) ? null : adapterManager.loadAdapter(this, adapter.getName());
}
public long getFileSize() {
return request.getFileSize();
}
public String getMimeType() {
return request.getMimeType();
}
public String toString() {
final StringBuffer buf = new StringBuffer("FileTransferInfo[");
buf.append("file=").append(f);
buf.append(";size=").append(getFileSize());
buf.append(";description=" + getDescription());
buf.append(";mimeType=").append(getMimeType()).append("]");
return buf.toString();
}
};
public IIncomingFileTransfer accept(File localFileToSave) throws IncomingFileTransferException {
try {
final OutputStream outs = new FileOutputStream(localFileToSave);
return accept(outs, new IFileTransferListener() {
public void handleTransferEvent(IFileTransferEvent event) {
if (event instanceof IIncomingFileTransferReceiveDoneEvent) {
try {
outs.close();
} catch (final IOException e) {
}
}
}
});
} catch (final FileNotFoundException e) {
throw new IncomingFileTransferException("Exception opening file for writing", e);
}
}
public IFileTransferInfo getFileTransferInfo() {
return fileTransferInfo;
}
public ID getRequesterID() {
return createIDFromName(request.getRequestor());
}
public void reject() {
request.reject();
}
public boolean requestAccepted() {
return requestAccepted;
}
public String toString() {
final StringBuffer buf = new StringBuffer("FileTransferRequestEvent[");
buf.append("requester=").append(getRequesterID());
buf.append(";requestAccepted=").append(requestAccepted());
buf.append(";transferInfo=").append(getFileTransferInfo()).append("]");
return buf.toString();
}
public IIncomingFileTransfer accept(OutputStream outputStream, IFileTransferListener listener) throws IncomingFileTransferException {
if (requestAccepted)
throw new IncomingFileTransferException("Incoming request previously accepted");
if (outputStream == null)
throw new IncomingFileTransferException("outputStream cannot be null");
incoming = request.accept();
requestAccepted = true;
try {
return new XMPPIncomingFileTransfer(IDFactory.getDefault().createStringID(request.getStreamID()), request.getFileName(), incoming.recieveFile(), outputStream, request.getFileSize(), listener);
} catch (final Exception e) {
throw new IncomingFileTransferException("Exception receiving file", e);
}
}
});
}
Aggregations