Search in sources :

Example 11 with IAdapterManager

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));
}
Also used : IAdaptable(org.eclipse.core.runtime.IAdaptable) IAdapterManager(org.eclipse.core.runtime.IAdapterManager)

Example 12 with IAdapterManager

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"));
}
Also used : IAdapterFactory(org.eclipse.core.runtime.IAdapterFactory) IAdapterManager(org.eclipse.core.runtime.IAdapterManager)

Example 13 with IAdapterManager

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"));
}
Also used : IAdapterFactory(org.eclipse.core.runtime.IAdapterFactory) IAdapterManager(org.eclipse.core.runtime.IAdapterManager)

Example 14 with IAdapterManager

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;
}
Also used : Status(org.eclipse.core.runtime.Status) IStatus(org.eclipse.core.runtime.IStatus) ServiceTracker(org.osgi.util.tracker.ServiceTracker) IAdapterManager(org.eclipse.core.runtime.IAdapterManager)

Example 15 with IAdapterManager

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);
            }
        }
    });
}
Also used : HashMap(java.util.HashMap) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileNotFoundException(java.io.FileNotFoundException) IFileTransferInfo(org.eclipse.ecf.filetransfer.IFileTransferInfo) IFileTransferListener(org.eclipse.ecf.filetransfer.IFileTransferListener) IOException(java.io.IOException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) IncomingFileTransferException(org.eclipse.ecf.filetransfer.IncomingFileTransferException) IIncomingFileTransfer(org.eclipse.ecf.filetransfer.IIncomingFileTransfer) IFileTransferEvent(org.eclipse.ecf.filetransfer.events.IFileTransferEvent) FileOutputStream(java.io.FileOutputStream) IIncomingFileTransferReceiveDoneEvent(org.eclipse.ecf.filetransfer.events.IIncomingFileTransferReceiveDoneEvent) IAdapterManager(org.eclipse.core.runtime.IAdapterManager) IncomingFileTransferException(org.eclipse.ecf.filetransfer.IncomingFileTransferException) XMPPID(org.eclipse.ecf.provider.xmpp.identity.XMPPID) ID(org.eclipse.ecf.core.identity.ID) HashMap(java.util.HashMap) Map(java.util.Map) File(java.io.File) IFileTransferRequestEvent(org.eclipse.ecf.filetransfer.events.IFileTransferRequestEvent)

Aggregations

IAdapterManager (org.eclipse.core.runtime.IAdapterManager)16 ServiceTracker (org.osgi.util.tracker.ServiceTracker)4 IAdapterFactory (org.eclipse.core.runtime.IAdapterFactory)3 CoreException (org.eclipse.core.runtime.CoreException)2 IStatus (org.eclipse.core.runtime.IStatus)2 Status (org.eclipse.core.runtime.Status)2 File (java.io.File)1 FileNotFoundException (java.io.FileNotFoundException)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 OutputStream (java.io.OutputStream)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 ICountable (org.eclipse.core.expressions.ICountable)1 IIterable (org.eclipse.core.expressions.IIterable)1 IAdaptable (org.eclipse.core.runtime.IAdaptable)1 ID (org.eclipse.ecf.core.identity.ID)1 AdapterManagerTracker (org.eclipse.ecf.core.util.AdapterManagerTracker)1 IFileTransferInfo (org.eclipse.ecf.filetransfer.IFileTransferInfo)1 IFileTransferListener (org.eclipse.ecf.filetransfer.IFileTransferListener)1