ConnectorInfoProviderService.java revision 88f2d7061bb42999901dcff81c37089b000d32e0
9e201d78ac6efdd08550263f2e146cdb76e9d49bvboxsyncpackage org.forgerock.openidm.provisioner.openicf.impl;
9e201d78ac6efdd08550263f2e146cdb76e9d49bvboxsync
9e201d78ac6efdd08550263f2e146cdb76e9d49bvboxsyncimport org.apache.felix.scr.annotations.*;
9e201d78ac6efdd08550263f2e146cdb76e9d49bvboxsyncimport org.apache.felix.scr.annotations.Properties;
9e201d78ac6efdd08550263f2e146cdb76e9d49bvboxsyncimport org.forgerock.json.fluent.JsonValue;
9e201d78ac6efdd08550263f2e146cdb76e9d49bvboxsyncimport org.forgerock.json.fluent.JsonValueException;
c98fb3e16fcd571a790eab772c0c66173d225205vboxsyncimport org.forgerock.json.fluent.JsonPointer;
e90bc1b0a8fb83acc1c1dc65176f143163cbb1a4vboxsyncimport org.forgerock.openicf.framework.api.osgi.ConnectorManager;
c98fb3e16fcd571a790eab772c0c66173d225205vboxsyncimport org.forgerock.openidm.config.EnhancedConfig;
c98fb3e16fcd571a790eab772c0c66173d225205vboxsyncimport org.forgerock.openidm.config.JSONEnhancedConfig;
c98fb3e16fcd571a790eab772c0c66173d225205vboxsyncimport org.forgerock.openidm.core.IdentityServer;
c98fb3e16fcd571a790eab772c0c66173d225205vboxsyncimport org.forgerock.openidm.core.ServerConstants;
c98fb3e16fcd571a790eab772c0c66173d225205vboxsyncimport org.forgerock.openidm.metadata.MetaDataProvider;
c98fb3e16fcd571a790eab772c0c66173d225205vboxsyncimport org.forgerock.openidm.metadata.WaitForMetaData;
c98fb3e16fcd571a790eab772c0c66173d225205vboxsyncimport org.forgerock.openidm.provisioner.openicf.ConnectorInfoProvider;
9e201d78ac6efdd08550263f2e146cdb76e9d49bvboxsyncimport org.forgerock.openidm.provisioner.openicf.ConnectorReference;
9e201d78ac6efdd08550263f2e146cdb76e9d49bvboxsyncimport org.forgerock.openidm.provisioner.openicf.commons.ConnectorUtil;
9e201d78ac6efdd08550263f2e146cdb76e9d49bvboxsyncimport org.identityconnectors.common.StringUtil;
9e201d78ac6efdd08550263f2e146cdb76e9d49bvboxsyncimport org.identityconnectors.framework.api.*;
9e201d78ac6efdd08550263f2e146cdb76e9d49bvboxsyncimport org.identityconnectors.framework.api.operations.SchemaApiOp;
9e201d78ac6efdd08550263f2e146cdb76e9d49bvboxsyncimport org.identityconnectors.framework.api.operations.TestApiOp;
9e201d78ac6efdd08550263f2e146cdb76e9d49bvboxsyncimport org.identityconnectors.framework.impl.api.APIConfigurationImpl;
9e201d78ac6efdd08550263f2e146cdb76e9d49bvboxsyncimport org.identityconnectors.framework.impl.api.AbstractConnectorInfo;
9e201d78ac6efdd08550263f2e146cdb76e9d49bvboxsyncimport org.identityconnectors.framework.impl.api.remote.RemoteConnectorInfoImpl;
9e201d78ac6efdd08550263f2e146cdb76e9d49bvboxsyncimport org.osgi.framework.Constants;
4896726d903f56fe43eb13e2ba27ae1d63b8c40evboxsyncimport org.osgi.service.component.ComponentContext;
4896726d903f56fe43eb13e2ba27ae1d63b8c40evboxsyncimport org.osgi.service.component.ComponentException;
9e201d78ac6efdd08550263f2e146cdb76e9d49bvboxsyncimport org.slf4j.Logger;
9e201d78ac6efdd08550263f2e146cdb76e9d49bvboxsyncimport org.slf4j.LoggerFactory;
9e201d78ac6efdd08550263f2e146cdb76e9d49bvboxsync
9e201d78ac6efdd08550263f2e146cdb76e9d49bvboxsyncimport java.io.*;
9e201d78ac6efdd08550263f2e146cdb76e9d49bvboxsyncimport java.lang.reflect.UndeclaredThrowableException;
9e201d78ac6efdd08550263f2e146cdb76e9d49bvboxsyncimport java.net.MalformedURLException;
9e201d78ac6efdd08550263f2e146cdb76e9d49bvboxsyncimport java.net.URISyntaxException;
9e201d78ac6efdd08550263f2e146cdb76e9d49bvboxsyncimport java.net.URL;
9e201d78ac6efdd08550263f2e146cdb76e9d49bvboxsyncimport java.net.URLDecoder;
9e201d78ac6efdd08550263f2e146cdb76e9d49bvboxsyncimport java.util.*;
9e201d78ac6efdd08550263f2e146cdb76e9d49bvboxsyncimport java.util.jar.JarEntry;
9e201d78ac6efdd08550263f2e146cdb76e9d49bvboxsyncimport java.util.jar.JarInputStream;
9e201d78ac6efdd08550263f2e146cdb76e9d49bvboxsync
9e201d78ac6efdd08550263f2e146cdb76e9d49bvboxsync/**
9e201d78ac6efdd08550263f2e146cdb76e9d49bvboxsync * ConnectorInfoProviderService
9e201d78ac6efdd08550263f2e146cdb76e9d49bvboxsync *
9e201d78ac6efdd08550263f2e146cdb76e9d49bvboxsync * @author $author$
9e201d78ac6efdd08550263f2e146cdb76e9d49bvboxsync * @version $Revision$ $Date$
4e46eee5a96078ebec6cb36939b794a391f1ade0vboxsync */
4e46eee5a96078ebec6cb36939b794a391f1ade0vboxsync@Component(name = ConnectorInfoProviderService.PID, policy = ConfigurationPolicy.OPTIONAL, description = "OpenICF Connector Info Service", immediate = true)
4e46eee5a96078ebec6cb36939b794a391f1ade0vboxsync@Service
e90bc1b0a8fb83acc1c1dc65176f143163cbb1a4vboxsync@Properties({
9e201d78ac6efdd08550263f2e146cdb76e9d49bvboxsync @Property(name = Constants.SERVICE_VENDOR, value = ServerConstants.SERVER_VENDOR_NAME),
9e201d78ac6efdd08550263f2e146cdb76e9d49bvboxsync @Property(name = Constants.SERVICE_DESCRIPTION, value = "OpenICF Connector Info Service")
9e201d78ac6efdd08550263f2e146cdb76e9d49bvboxsync})
9e201d78ac6efdd08550263f2e146cdb76e9d49bvboxsyncpublic class ConnectorInfoProviderService implements ConnectorInfoProvider, MetaDataProvider {
9e201d78ac6efdd08550263f2e146cdb76e9d49bvboxsync private final static Logger TRACE = LoggerFactory.getLogger(ConnectorInfoProviderService.class);
9e201d78ac6efdd08550263f2e146cdb76e9d49bvboxsync
9e201d78ac6efdd08550263f2e146cdb76e9d49bvboxsync
9e201d78ac6efdd08550263f2e146cdb76e9d49bvboxsync //Public Constants
9e201d78ac6efdd08550263f2e146cdb76e9d49bvboxsync public static final String DEFAULT_CONNECTORS_LOCATION = "connectors";
9e201d78ac6efdd08550263f2e146cdb76e9d49bvboxsync public static final String PROPERTY_OPENICF_CONNECTOR_URL = "connectorsLocation";
9e201d78ac6efdd08550263f2e146cdb76e9d49bvboxsync public static final String PID = "org.forgerock.openidm.provisioner.openicf.connectorinfoprovider";
0edd5583e49bb46471ed27200f0fe2fdf00f5ecavboxsync
e06cbb510db6f327856cabc016398e4fda505809vboxsync //Private
e06cbb510db6f327856cabc016398e4fda505809vboxsync private Map<String, RemoteFrameworkConnectionInfo> remoteFrameworkConnectionInfo = new HashMap<String, RemoteFrameworkConnectionInfo>();
e06cbb510db6f327856cabc016398e4fda505809vboxsync private URL[] connectorURLs = null;
10f224d85cd1135f0ca4ee1c1c1a7e15988bed58vboxsync /*
10f224d85cd1135f0ca4ee1c1c1a7e15988bed58vboxsync * If this instance was instantiated for MetaDataProvider by Class#newInstance then this is false.
10f224d85cd1135f0ca4ee1c1c1a7e15988bed58vboxsync * If this instance was activated by OSGi SCR then this is true.
9e201d78ac6efdd08550263f2e146cdb76e9d49bvboxsync */
10f224d85cd1135f0ca4ee1c1c1a7e15988bed58vboxsync private boolean isOSGiServiceInstance = false;
10f224d85cd1135f0ca4ee1c1c1a7e15988bed58vboxsync
10f224d85cd1135f0ca4ee1c1c1a7e15988bed58vboxsync /**
607049337dcf359292c82c9372a2c19d9c4cdd2fvboxsync * ConnectorManager service.
607049337dcf359292c82c9372a2c19d9c4cdd2fvboxsync */
607049337dcf359292c82c9372a2c19d9c4cdd2fvboxsync @Reference(
607049337dcf359292c82c9372a2c19d9c4cdd2fvboxsync name = "ref_ConnectorManager_ConnectorInfoProvider",
10f224d85cd1135f0ca4ee1c1c1a7e15988bed58vboxsync referenceInterface = ConnectorManager.class,
10f224d85cd1135f0ca4ee1c1c1a7e15988bed58vboxsync bind = "bind",
48eca14ff165b52c459d7c3090f1f5dba0d1fedbvboxsync unbind = "unbind",
9e201d78ac6efdd08550263f2e146cdb76e9d49bvboxsync cardinality = ReferenceCardinality.OPTIONAL_UNARY,
9e201d78ac6efdd08550263f2e146cdb76e9d49bvboxsync policy = ReferencePolicy.STATIC)
9e201d78ac6efdd08550263f2e146cdb76e9d49bvboxsync private ConnectorManager osgiConnectorManager = null;
48eca14ff165b52c459d7c3090f1f5dba0d1fedbvboxsync
48eca14ff165b52c459d7c3090f1f5dba0d1fedbvboxsync
9e201d78ac6efdd08550263f2e146cdb76e9d49bvboxsync protected void bind(ConnectorManager service) {
9e201d78ac6efdd08550263f2e146cdb76e9d49bvboxsync TRACE.info("ConnectorManager is bound.");
9e201d78ac6efdd08550263f2e146cdb76e9d49bvboxsync this.osgiConnectorManager = service;
9e201d78ac6efdd08550263f2e146cdb76e9d49bvboxsync
9e201d78ac6efdd08550263f2e146cdb76e9d49bvboxsync }
9e201d78ac6efdd08550263f2e146cdb76e9d49bvboxsync
9e201d78ac6efdd08550263f2e146cdb76e9d49bvboxsync protected void unbind(ConnectorManager service) {
9e201d78ac6efdd08550263f2e146cdb76e9d49bvboxsync this.osgiConnectorManager = null;
9e201d78ac6efdd08550263f2e146cdb76e9d49bvboxsync TRACE.info("ConnectorManager is unbound.");
9e201d78ac6efdd08550263f2e146cdb76e9d49bvboxsync }
9e201d78ac6efdd08550263f2e146cdb76e9d49bvboxsync
9e201d78ac6efdd08550263f2e146cdb76e9d49bvboxsync
9e201d78ac6efdd08550263f2e146cdb76e9d49bvboxsync @Activate
9e201d78ac6efdd08550263f2e146cdb76e9d49bvboxsync protected void activate(ComponentContext context) {
9e201d78ac6efdd08550263f2e146cdb76e9d49bvboxsync TRACE.trace("Activating Service with configuration {}", context.getProperties());
9e201d78ac6efdd08550263f2e146cdb76e9d49bvboxsync JsonValue configuration = getConfiguration(context);
9e201d78ac6efdd08550263f2e146cdb76e9d49bvboxsync
9e201d78ac6efdd08550263f2e146cdb76e9d49bvboxsync // Create a single instance of ConnectorInfoManagerFactory
9e201d78ac6efdd08550263f2e146cdb76e9d49bvboxsync ConnectorInfoManagerFactory factory = ConnectorInfoManagerFactory.getInstance();
9e201d78ac6efdd08550263f2e146cdb76e9d49bvboxsync try {
9e201d78ac6efdd08550263f2e146cdb76e9d49bvboxsync // String connectorLocation = DEFAULT_CONNECTORS_LOCATION;
9e201d78ac6efdd08550263f2e146cdb76e9d49bvboxsync String connectorLocation = configuration.get(PROPERTY_OPENICF_CONNECTOR_URL).defaultTo(DEFAULT_CONNECTORS_LOCATION).asString();
9e201d78ac6efdd08550263f2e146cdb76e9d49bvboxsync // Initialise Local ConnectorInfoManager
22cf3a5eb5a3cc2ea58e4fe974914e6a32514614vboxsync initialiseLocalManager(factory, connectorLocation);
9e201d78ac6efdd08550263f2e146cdb76e9d49bvboxsync } catch (JsonValueException e) {
9e201d78ac6efdd08550263f2e146cdb76e9d49bvboxsync TRACE.error("Invalid configuration {}", configuration.getValue(), e);
9e201d78ac6efdd08550263f2e146cdb76e9d49bvboxsync throw new ComponentException("Invalid configuration, service can not be started", e);
10f224d85cd1135f0ca4ee1c1c1a7e15988bed58vboxsync }
9e201d78ac6efdd08550263f2e146cdb76e9d49bvboxsync
9e201d78ac6efdd08550263f2e146cdb76e9d49bvboxsync
9e201d78ac6efdd08550263f2e146cdb76e9d49bvboxsync JsonValue remoteConnectorHosts = null;
10f224d85cd1135f0ca4ee1c1c1a7e15988bed58vboxsync try {
10f224d85cd1135f0ca4ee1c1c1a7e15988bed58vboxsync remoteConnectorHosts = configuration.get(ConnectorUtil.OPENICF_REMOTE_CONNECTOR_SERVERS).expect(List.class);
10f224d85cd1135f0ca4ee1c1c1a7e15988bed58vboxsync if (!remoteConnectorHosts.isNull()) {
607049337dcf359292c82c9372a2c19d9c4cdd2fvboxsync initialiseRemoteManager(factory, remoteConnectorHosts);
607049337dcf359292c82c9372a2c19d9c4cdd2fvboxsync }
607049337dcf359292c82c9372a2c19d9c4cdd2fvboxsync } catch (JsonValueException e) {
607049337dcf359292c82c9372a2c19d9c4cdd2fvboxsync TRACE.error("Invalid configuration remoteConnectorHosts must be list or null. {}", remoteConnectorHosts, e);
10f224d85cd1135f0ca4ee1c1c1a7e15988bed58vboxsync throw new ComponentException("Invalid configuration, service can not be started", e);
10f224d85cd1135f0ca4ee1c1c1a7e15988bed58vboxsync }
9e201d78ac6efdd08550263f2e146cdb76e9d49bvboxsync isOSGiServiceInstance = true;
9e201d78ac6efdd08550263f2e146cdb76e9d49bvboxsync TRACE.info("Component is activated.");
9e201d78ac6efdd08550263f2e146cdb76e9d49bvboxsync }
9e201d78ac6efdd08550263f2e146cdb76e9d49bvboxsync
9e201d78ac6efdd08550263f2e146cdb76e9d49bvboxsync protected void initialiseRemoteManager(ConnectorInfoManagerFactory factory, JsonValue remoteConnectorHosts) throws JsonValueException {
9e201d78ac6efdd08550263f2e146cdb76e9d49bvboxsync for (Object o : remoteConnectorHosts.asList()) {
9e201d78ac6efdd08550263f2e146cdb76e9d49bvboxsync if (o instanceof Map) {
9e201d78ac6efdd08550263f2e146cdb76e9d49bvboxsync Map<String, Object> info = (Map<String, Object>) o;
9e201d78ac6efdd08550263f2e146cdb76e9d49bvboxsync try {
9e201d78ac6efdd08550263f2e146cdb76e9d49bvboxsync RemoteFrameworkConnectionInfo rfi = ConnectorUtil.getRemoteFrameworkConnectionInfo(info);
9e201d78ac6efdd08550263f2e146cdb76e9d49bvboxsync String name = (String) info.get("name");
9e201d78ac6efdd08550263f2e146cdb76e9d49bvboxsync if (StringUtil.isNotBlank(name) && null != rfi) {
9e201d78ac6efdd08550263f2e146cdb76e9d49bvboxsync try {
9e201d78ac6efdd08550263f2e146cdb76e9d49bvboxsync remoteFrameworkConnectionInfo.put(name, rfi);
9e201d78ac6efdd08550263f2e146cdb76e9d49bvboxsync factory.getRemoteManager(rfi);
9e201d78ac6efdd08550263f2e146cdb76e9d49bvboxsync } catch (Exception e) {
9e201d78ac6efdd08550263f2e146cdb76e9d49bvboxsync TRACE.error("Remote ConnectorServer: {} initialization failed.", rfi, e);
9e201d78ac6efdd08550263f2e146cdb76e9d49bvboxsync }
9e201d78ac6efdd08550263f2e146cdb76e9d49bvboxsync } else {
TRACE.error("RemoteFrameworkConnectionInfo has no name");
}
} catch (IllegalArgumentException e) {
TRACE.error("RemoteFrameworkConnectionInfo can not be read", e);
}
}
}
}
protected void initialiseLocalManager(ConnectorInfoManagerFactory factory, String connectorsArea) {
try {
String connectorsDir = URLDecoder.decode(connectorsArea, "UTF-8");
TRACE.debug("Using connectors from [" + connectorsDir + "]");
File dir = IdentityServer.getFileForPath(connectorsDir);
//This is a fix to support absolute path on OSX
if (!dir.exists()) {
String absolutePath = dir.getAbsolutePath();
if (!absolutePath.endsWith(File.separator)) {
dir = new File(absolutePath.concat(File.separator));
}
}
if (!dir.exists()) {
String absolutePath = dir.getAbsolutePath();
TRACE.error("Configuration area [" + absolutePath + "] does not exist. Unable to load connectors.");
} else {
try {
TRACE.debug("Looking for connectors in {} directory.", dir.getAbsoluteFile().toURI().toURL());
URL[] bundleUrls = getConnectorURLs(dir.getAbsoluteFile().toURI().toURL());
factory.getLocalManager(bundleUrls);
} catch (MalformedURLException e) {
TRACE.error("How can this happen?", e);
}
}
} catch (UnsupportedEncodingException e) {
// Should never happen.
throw new UndeclaredThrowableException(e);
} catch (Throwable t) {
TRACE.error("LocalManager initialisation for {} failed.", connectorsArea, t);
throw new ComponentException("LocalManager initialisation failed.", t);
}
}
@Deactivate
protected void deactivate(ComponentContext context) {
TRACE.trace("Deactivating Service {}", context.getProperties());
ConnectorInfoManagerFactory factory = ConnectorInfoManagerFactory.getInstance();
factory.clearLocalCache();
connectorURLs = null;
factory.clearRemoteCache();
remoteFrameworkConnectionInfo.clear();
TRACE.info("Component is deactivated.");
}
// @Modified
// protected void update(ComponentContext context) {
// }
/**
* {@inheritDoc}
*/
public ConnectorInfo findConnectorInfo(ConnectorReference connectorReference) {
ConnectorInfoManager connectorInfoManager = null;
ConnectorInfo connectorInfo = null;
ConnectorInfoManagerFactory factory = ConnectorInfoManagerFactory.getInstance();
if (ConnectorReference.SINGLE_LOCAL_CONNECTOR_MANAGER.equals(connectorReference.getConnectorHost())) {
connectorInfoManager = factory.getLocalManager(getConnectorURLs());
} else if (ConnectorReference.OSGI_SERVICE_CONNECTOR_MANAGER.equals(connectorReference.getConnectorHost())) {
connectorInfoManager = osgiConnectorManager;
} else {
RemoteFrameworkConnectionInfo rfci = remoteFrameworkConnectionInfo.get(connectorReference.getConnectorHost());
if (null != rfci) {
connectorInfoManager = factory.getRemoteManager(rfci);
}
}
if (null != connectorInfoManager) {
try {
connectorInfo = connectorInfoManager.findConnectorInfo(connectorReference.getConnectorKey());
} catch (Exception e) {
TRACE.error("Can not find ConnectorInfo for {}", connectorReference, e);
}
}
return connectorInfo;
}
/**
* {@inheritDoc}
*/
public List<ConnectorInfo> getAllConnectorInfo() {
ConnectorInfoManagerFactory factory = ConnectorInfoManagerFactory.getInstance();
ConnectorInfoManager connectorInfoManager = factory.getLocalManager(getConnectorURLs());
List<ConnectorInfo> result = new ArrayList<ConnectorInfo>(connectorInfoManager.getConnectorInfos());
if (null != osgiConnectorManager) {
result.addAll(osgiConnectorManager.getConnectorInfos());
}
for (RemoteFrameworkConnectionInfo entry : remoteFrameworkConnectionInfo.values()) {
try {
ConnectorInfoManager remoteConnectorInfoManager = factory.getRemoteManager(entry);
result.addAll(remoteConnectorInfoManager.getConnectorInfos());
} catch (Exception e) {
TRACE.error("Remote Connector Server is not available for {}", entry, e);
}
}
return Collections.unmodifiableList(result);
}
/**
* {@inheritDoc}
*
* @throws org.identityconnectors.framework.common.exceptions.ConnectorException
* if OpenICF failed to create new
* connector facade
*/
public void testConnector(APIConfiguration configuration) {
ConnectorFacadeFactory connectorFacadeFactory = ConnectorFacadeFactory.getInstance();
ConnectorFacade facade = connectorFacadeFactory.newInstance(configuration);
if (null == facade && null != osgiConnectorManager) {
try {
facade = osgiConnectorManager.newInstance(configuration);
} catch (Exception e) {
TRACE.warn("OSGi ConnectorManager can not create ConnectorFacade", e);
}
}
if (null != facade) {
TestApiOp operation = (TestApiOp) facade.getOperation(TestApiOp.class);
if (null != operation) {
operation.test();
}
}
throw new UnsupportedOperationException("ConnectorFacade can not be initialised");
}
/**
* {@inheritDoc}
*/
public Map<String, Object> createSystemConfiguration(APIConfiguration configuration, boolean validate) {
ConnectorFacadeFactory connectorFacadeFactory = ConnectorFacadeFactory.getInstance();
ConnectorFacade facade = connectorFacadeFactory.newInstance(configuration);
if (null == facade && null != osgiConnectorManager) {
try {
facade = osgiConnectorManager.newInstance(configuration);
} catch (Exception e) {
TRACE.warn("OSGi ConnectorManager can not create ConnectorFacade", e);
}
}
if (null != facade) {
Map<String, Object> jsonConfiguration = new LinkedHashMap<String, Object>();
APIConfigurationImpl impl = (APIConfigurationImpl) configuration;
AbstractConnectorInfo connectorInfo = impl.getConnectorInfo();
ConnectorReference connectorReference = null;
if (connectorInfo instanceof RemoteConnectorInfoImpl) {
RemoteConnectorInfoImpl remoteInfo = (RemoteConnectorInfoImpl) connectorInfo;
for (Map.Entry<String, RemoteFrameworkConnectionInfo> entry : remoteFrameworkConnectionInfo.entrySet()) {
if (entry.getValue().equals(remoteInfo.getRemoteConnectionInfo())) {
connectorReference = new ConnectorReference(connectorInfo.getConnectorKey(), entry.getKey());
break;
}
}
} else {
connectorReference = new ConnectorReference(connectorInfo.getConnectorKey());
}
ConnectorUtil.setConnectorReference(connectorReference, jsonConfiguration);
ConnectorUtil.createSystemConfigurationFromAPIConfiguration(configuration, jsonConfiguration);
if (validate && facade.getSupportedOperations().contains(TestApiOp.class)) {
facade.test();
}
if (facade.getSupportedOperations().contains(SchemaApiOp.class)) {
ConnectorUtil.setObjectAndOperationConfiguration(facade.schema(), jsonConfiguration);
}
return jsonConfiguration;
}
throw new UnsupportedOperationException("ConnectorFacade can not be initialised");
}
/**
* {@inheritDoc}
*/
public List<JsonPointer> getPropertiesToEncrypt(String pidOrFactory, String instanceAlias, JsonValue config) throws WaitForMetaData {
List<JsonPointer> result = null;
if (null != pidOrFactory && null != config) {
if (PID.equals(pidOrFactory)) {
try {
JsonValue remoteConnectorHosts = config.get(ConnectorUtil.OPENICF_REMOTE_CONNECTOR_SERVERS).expect(List.class);
if (!remoteConnectorHosts.isNull()) {
result = new ArrayList<JsonPointer>(remoteConnectorHosts.size());
for (JsonValue hostConfig : remoteConnectorHosts) {
result.add(hostConfig.get(ConnectorUtil.OPENICF_KEY).getPointer());
}
}
} catch (JsonValueException e) {
TRACE.error("Invalid configuration remoteConnectorHosts must be list or null.", e);
}
} else if (OpenICFProvisionerService.PID.equals(pidOrFactory)) {
if (isOSGiServiceInstance) {
ConfigurationProperties properties = null;
try {
ConnectorReference connectorReference = ConnectorUtil.getConnectorReference(config);
ConnectorInfo ci = findConnectorInfo(connectorReference);
if (null != ci) {
properties = ci.createDefaultAPIConfiguration().getConfigurationProperties();
}
} catch (Exception e) {
TRACE.error("Failed to parse the config of {}-{}", new Object[]{pidOrFactory, instanceAlias}, e);
}
if (null != properties) {
JsonPointer configurationProperties = new JsonPointer(ConnectorUtil.OPENICF_CONFIGURATION_PROPERTIES);
result = new ArrayList<JsonPointer>(properties.getPropertyNames().size());
for (String name : properties.getPropertyNames()) {
ConfigurationProperty property = properties.getProperty(name);
if (property.isConfidential()) {
result.add(configurationProperties.child(name));
}
}
} else {
throw new WaitForMetaData(pidOrFactory);
}
} else {
throw new WaitForMetaData("Wait for the MetaDataProvider service instance");
}
}
}
return result;
}
private URL[] getConnectorURLs(URL... resourceURLs) {
if (null == connectorURLs) {
Set<URL> _bundleURLs = new HashSet<URL>();
for (URL resourceURL : resourceURLs) {
try {
Vector<URL> urls = null;
if ("file".equals(resourceURL.getProtocol())) {
File file = new File(resourceURL.toURI());
if (file.isDirectory()) {
FileFilter filter = new FileFilter() {
public boolean accept(File f) {
return (f.isDirectory()) || (f.getName().endsWith(".jar"));
}
};
File[] files = file.listFiles(filter);
urls = new Vector<URL>(files.length);
for (File subFile : files) {
String fname = subFile.getName();
TRACE.trace("Load Connector Bundle: {}", fname);
urls.add(new URL(resourceURL, fname));
}
}
} else if (("jar".equals(resourceURL.getProtocol())) || ("wsjar".equals(resourceURL.getProtocol()))) {
urls = getJarFileListing(resourceURL, "^META-INF/" + DEFAULT_CONNECTORS_LOCATION + "/(.*).jar$");
} else {
TRACE.info("Local connector support disabled. No support for bundle URLs with protocol {}", resourceURL.getProtocol());
}
if ((urls == null) || (urls.size() == 0)) {
TRACE.info("No local connector bundles found within {}", resourceURL);
}
if (null != urls) {
_bundleURLs.addAll(urls);
}
} catch (IOException ex) {
//TODO Add Message
TRACE.error("XXX", ex);
} catch (URISyntaxException e) {
TRACE.error("URL instance does not comply with RFC 2396", e);
}
}
if (TRACE.isDebugEnabled()) {
for (URL u : _bundleURLs) {
TRACE.debug("Connector URL: {}", u);
}
}
connectorURLs = _bundleURLs.toArray(new URL[_bundleURLs.size()]);
}
return connectorURLs;
}
/**
* <p>Retrieve a list of filepaths from a given directory within a jar
* file. If filtered results are needed, you can supply a |filter|
* regular expression which will match each entry.
*
* @param jarLocation
* @param filter to filter the results within a regular expression.
* @return a list of files within the jar |file|
*/
private static Vector<URL> getJarFileListing(URL jarLocation, String filter) {
Vector<URL> files = new Vector<URL>();
if (jarLocation == null) {
return files; // Empty.
}
//strip out the file: and the !META-INF/bundles so only the JAR file left
String jarPath = jarLocation.getPath().substring(5, jarLocation.getPath().indexOf("!"));
try {
// Lets stream the jar file
JarInputStream jarInputStream = new JarInputStream(new FileInputStream(jarPath));
JarEntry jarEntry;
// Iterate the jar entries within that jar. Then make sure it follows the
// filter given from the user.
do {
jarEntry = jarInputStream.getNextJarEntry();
if (jarEntry != null) {
String fileName = jarEntry.getName();
// The filter could be null or has a matching regular expression.
if (filter == null || fileName.matches(filter)) {
files.add(new URL(jarLocation, fileName.replace(DEFAULT_CONNECTORS_LOCATION, "")));
}
}
} while (jarEntry != null);
jarInputStream.close();
} catch (IOException ioe) {
throw new RuntimeException("Unable to get Jar input stream from '" + jarLocation + "'", ioe);
}
return files;
}
private JsonValue getConfiguration(ComponentContext componentContext) {
EnhancedConfig enhancedConfig = new JSONEnhancedConfig();
return new JsonValue(enhancedConfig.getConfiguration(componentContext));
}
}