package org.apache.cxf.dosgi.dsw.handlers;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.cxf.common.logging.LogUtils;
import org.apache.cxf.dosgi.dsw.service.ImportRegistrationImpl;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceFactory;
import org.osgi.framework.ServiceRegistration;
import org.osgi.service.remoteserviceadmin.EndpointDescription;
private static final Logger LOG = LogUtils.getL7dLogger(ClientServiceFactory.class);
private BundleContext dswContext;
private Class<?> iClass;
private EndpointDescription sd;
private ConfigurationTypeHandler handler;
private ImportRegistrationImpl importRegistartion;
private boolean closeable = false;
private int serviceCounter = 0;
ConfigurationTypeHandler handler, ImportRegistrationImpl ir) {
this.dswContext = dswContext;
this.iClass = iClass;
this.sd = sd;
this.handler = handler;
importRegistartion = ir;
}
public Object
getService(
final Bundle requestingBundle,
final ServiceRegistration sreg) {
String interfaceName = sd.getInterfaces() != null && sd.getInterfaces().size() > 0 ? (String)sd
.getInterfaces().toArray()[0] : null;
LOG.fine("getService() from serviceFactory for " + interfaceName);
try {
Object proxy = AccessController.doPrivileged(new PrivilegedAction<Object>() {
return handler.createProxy(sreg.getReference(), dswContext, requestingBundle
.getBundleContext(), iClass, sd);
}
});
synchronized (this) {
++serviceCounter;
}
return proxy;
} catch (IntentUnsatifiedException iue) {
LOG.info("Did not create proxy for " + interfaceName + " because intent " + iue.getIntent()
+ " could not be satisfied");
} catch (Exception ex) {
LOG.log(Level.WARNING, "Problem creating a remote proxy for " + interfaceName
+ " from CXF FindHook: ", ex);
}
return null;
}
public void ungetService(Bundle requestingBundle, ServiceRegistration sreg, Object serviceObject) {
StringBuilder sb = new StringBuilder();
sb.append("Releasing a client object");
Object objectClass = sreg.getReference().getProperty(org.osgi.framework.Constants.OBJECTCLASS);
if (objectClass != null) {
sb.append(", interfaces : ");
for (String s : (String[])objectClass) {
sb.append(" " + s);
}
}
LOG.info(sb.toString());
synchronized (this) {
--serviceCounter;
LOG.fine("Services still provided by this ServiceFactory: " + serviceCounter);
if (serviceCounter <= 0 && closeable)
remove();
}
}
importRegistartion.closeAll();
}
synchronized (this) {
this.closeable = closeable;
if (serviceCounter <= 0 && closeable)
remove();
}
}
return closeable;
}
}