/**
* Copyright (c) 2009 - 2011 Cloudsmith Inc. and other contributors, as listed below.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Itemis AG - initial API and implementation
* Cloudsmith - moved to non ui package
*
*/
package org.cloudsmith.geppetto.injectable.eclipse;
import java.util.Map;
import org.apache.log4j.Logger;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.IExecutableExtension;
import org.eclipse.core.runtime.IExecutableExtensionFactory;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.osgi.framework.Bundle;
import com.google.inject.Injector;
/**
* @author Sven Efftinge - Initial contribution and API
*/
public abstract class AbstractGuiceAwareExecutableExtensionFactory implements IExecutableExtensionFactory,
IExecutableExtension {
public static final String GUICEKEY = "guicekey";
protected Logger log = Logger.getLogger(getClass());
protected String clazzName;
protected IConfigurationElement config;
public Object create() throws CoreException {
try {
final Class<?> clazz = getBundle().loadClass(clazzName);
final Injector injector = getInjector();
final Object result = injector.getInstance(clazz);
if(result instanceof IExecutableExtension)
((IExecutableExtension) result).setInitializationData(config, null, null);
return result;
}
catch(Exception e) {
log.error(e);
throw new CoreException(new Status(IStatus.ERROR, getBundle().getSymbolicName(), e.getMessage() +
" ExtensionFactory: " + getClass().getName(), e));
}
}
protected abstract Bundle getBundle();
protected abstract Injector getInjector();
@SuppressWarnings({ "unchecked" })
public void setInitializationData(IConfigurationElement config, String propertyName, Object data)
throws CoreException {
if(data instanceof String) {
clazzName = (String) data;
}
else if(data instanceof Map<?, ?>) {
clazzName = ((Map<String, String>) data).get(GUICEKEY);
}
if(clazzName == null) {
throw new IllegalArgumentException("couldn't handle passed data : " + data);
}
this.config = config;
}
}