You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@directory.apache.org by rj...@apache.org on 2009/06/09 14:50:47 UTC
svn commit: r782980 [7/8] - in
/directory/sandbox/slp/src/main/java/org/apache/directory/slp: ./ codec/
extensions/ impl/ impl/da/ impl/filter/ messages/
Modified: directory/sandbox/slp/src/main/java/org/apache/directory/slp/impl/da/DirectoryAgentDaemonImpl.java
URL: http://svn.apache.org/viewvc/directory/sandbox/slp/src/main/java/org/apache/directory/slp/impl/da/DirectoryAgentDaemonImpl.java?rev=782980&r1=782979&r2=782980&view=diff
==============================================================================
--- directory/sandbox/slp/src/main/java/org/apache/directory/slp/impl/da/DirectoryAgentDaemonImpl.java (original)
+++ directory/sandbox/slp/src/main/java/org/apache/directory/slp/impl/da/DirectoryAgentDaemonImpl.java Tue Jun 9 12:50:45 2009
@@ -53,143 +53,164 @@
import org.apache.directory.slp.messages.ServiceTypeReplyMessage;
import org.apache.directory.slp.messages.ServiceTypeRequestMessage;
-
/**
*
* @author Lorenz Breu
*/
-public class DirectoryAgentDaemonImpl extends SLPCore implements DirectoryAgentDaemon{
-
+public class DirectoryAgentDaemonImpl extends SLPCore implements
+ DirectoryAgentDaemon {
+
private static int statelessBootTimestamp;
-
- //The storage mechanism holding the information on registered services
+
+ // The storage mechanism holding the information on registered services
private ServiceStore store;
-
+
private boolean running = false;
-
+
// The scopes supported by this DA
private List<String> myScopes = new ArrayList<String>();
-
+
/**
* Sorted set for disposal of services which lifetimes have expired:
*
* Long expirationTimestamp -> String service.
*/
- private SortedMap<Long,String> serviceDisposalQueue = new TreeMap<Long,String>();
-
+ private final SortedMap<Long, String> serviceDisposalQueue = new TreeMap<Long, String>();
+
// The attributes of the DA as specified in the configuration
- private String[] attributes = new String[]{};
-
- public DirectoryAgentDaemonImpl() throws Exception{
+ private String[] attributes = new String[] {};
+
+ public DirectoryAgentDaemonImpl() throws Exception {
statelessBootTimestamp = (int) System.currentTimeMillis();
- attributes = SLPUtils.stringToStringArray(SLPCore.CONFIG.getConfigDAAttributes(), ",");
+ attributes = SLPUtils.stringToStringArray(SLPCore.CONFIG
+ .getConfigDAAttributes(), ",");
// load the scopes this DA is responsible for
- myScopes = SLPUtils.stringToList(SLPCore.CONFIG.getScopes().toLowerCase(), ",");
- running=true;
-
- new UnsolicitedAdvertisementThread();
+ myScopes = SLPUtils.stringToList(SLPCore.CONFIG.getScopes()
+ .toLowerCase(), ",");
+ running = true;
+
+ new UnsolicitedAdvertisementThread();
new ServiceDisposalThread();
SLPCore.platform.logDebug("jSLP directory agent starting...");
-
+
}
-
-
- /* (non-Javadoc)
+
+ /*
+ * (non-Javadoc)
+ *
* @see ch.ethz.iks.slp.impl.da.DirectoryAgentDaemon#listServices()
*/
- public List<Service> listServices(){
+ public List<Service> listServices() {
try {
return store.listServices();
- } catch (ServiceLocationException sle){
+ } catch (final ServiceLocationException sle) {
return new ArrayList<Service>();
}
}
-
- /* (non-Javadoc)
- * @see ch.ethz.iks.slp.impl.da.DirectoryAgentDaemon#isInScope(java.lang.String)
+ /*
+ * (non-Javadoc)
+ *
+ * @see
+ * ch.ethz.iks.slp.impl.da.DirectoryAgentDaemon#isInScope(java.lang.String)
*/
- public boolean isInScope(String scope){
- for (String s: myScopes){
- if (s.toLowerCase().equals(scope.toLowerCase())){
+ public boolean isInScope(final String scope) {
+ for (final String s : myScopes) {
+ if (s.toLowerCase().equals(scope.toLowerCase())) {
return true;
}
}
return false;
}
-
- /* (non-Javadoc)
- * @see ch.ethz.iks.slp.impl.da.DirectoryAgentDaemon#isKnownService(ch.ethz.iks.slp.impl.Service)
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see
+ * ch.ethz.iks.slp.impl.da.DirectoryAgentDaemon#isKnownService(ch.ethz.iks
+ * .slp.impl.Service)
*/
- public boolean isKnownService(Service service){
- return (store.containsService(service.getURL().toString()));
+ public boolean isKnownService(final Service service) {
+ return store.containsService(service.getURL().toString());
}
-
- /* (non-Javadoc)
- * @see ch.ethz.iks.slp.impl.da.DirectoryAgentDaemon#handleMessage(ch.ethz.iks.slp.messages.AbstractSLPMessage)
+ /*
+ * (non-Javadoc)
+ *
+ * @see
+ * ch.ethz.iks.slp.impl.da.DirectoryAgentDaemon#handleMessage(ch.ethz.iks
+ * .slp.messages.AbstractSLPMessage)
*/
- public AbstractSLPReplyMessage handleMessage(AbstractSLPMessage message) throws ServiceLocationException{
+ public AbstractSLPReplyMessage handleMessage(
+ final AbstractSLPMessage message) throws ServiceLocationException {
if (message == null || !(message instanceof AbstractSLPMessage)) {
return null;
}
- AbstractSLPMessage msg = (AbstractSLPMessage) message;
- boolean scopeOverlap = stripUnsupportedScopes(msg);
+ final AbstractSLPMessage msg = message;
+ final boolean scopeOverlap = stripUnsupportedScopes(msg);
switch (msg.getFuncID()) {
case AbstractSLPMessage.DAADVERT:
- // for now drop message as the handler does the necessary things for UA/SA
-
- //TODO: keep a list of other DAs for coordination, replication, etc...
-
+ // for now drop message as the handler does the necessary things for
+ // UA/SA
+
+ // TODO: keep a list of other DAs for coordination, replication,
+ // etc...
+
return null;
-
+
// reply messages not care about, we do...
case AbstractSLPMessage.ATTRRPLY:
case AbstractSLPMessage.SRVRPLY:
case AbstractSLPMessage.SRVTYPERPLY:
-
+
return null;
// request messages
case AbstractSLPMessage.SRVRQST:
- if (msg.hasUnsupportedMandatoryExtensions()){
- ServiceReplyMessage servreply = new ServiceReplyMessage();
+ if (msg.hasUnsupportedMandatoryExtensions()) {
+ final ServiceReplyMessage servreply = new ServiceReplyMessage();
servreply.setXid(msg.getXid());
servreply.setLocale(msg.getLocale());
- servreply.setErrorCode(ServiceLocationException.OPTION_NOT_UNDERSTOOD);
+ servreply
+ .setErrorCode(ServiceLocationException.OPTION_NOT_UNDERSTOOD);
return servreply;
}
-
- ServiceRequestMessage srvReq = (ServiceRequestMessage) msg;
- if (!scopeOverlap){
+
+ final ServiceRequestMessage srvReq = (ServiceRequestMessage) msg;
+ if (!scopeOverlap) {
return null;
}
- if (srvReq.getServiceType().toString().equals("service:directory-agent")){
- for (String s:srvReq.getScopes()){
- if (isInScope(s)){
+ if (srvReq.getServiceType().toString().equals(
+ "service:directory-agent")) {
+ for (final String s : srvReq.getScopes()) {
+ if (isInScope(s)) {
try {
- DAAdvertisementMessage advert = buildAdvert();
+ final DAAdvertisementMessage advert = buildAdvert();
advert.setXid(srvReq.getXid());
- if (SLPCore.CONFIG.getSecurityEnabled()){
- List<String> mySpis = SLPUtils.stringToList(SLPCore.CONFIG.getSPI(), ",");
+ if (SLPCore.CONFIG.getSecurityEnabled()) {
+ final List<String> mySpis = SLPUtils
+ .stringToList(SLPCore.CONFIG.getSPI(),
+ ",");
boolean knownSpi = false;
- for (String spi : srvReq.getSPIs()){
- if (mySpis.contains(spi)){
+ for (final String spi : srvReq.getSPIs()) {
+ if (mySpis.contains(spi)) {
knownSpi = true;
break;
}
}
- if (knownSpi){
- advert.setSPIs(SLPUtils.stringToStringArray(SLPCore.CONFIG.getSPI(), ","));
+ if (knownSpi) {
+ advert.setSPIs(SLPUtils
+ .stringToStringArray(SLPCore.CONFIG
+ .getSPI(), ","));
advert.sign(SLPCore.CONFIG.getSPI());
return advert;
}
// spi not supported
- advert.setErrorCode(ServiceLocationException.AUTHENTICATION_UNKNOWN);
-
-
+ advert
+ .setErrorCode(ServiceLocationException.AUTHENTICATION_UNKNOWN);
+
}
return advert;
- } catch (ServiceLocationException sle){
+ } catch (final ServiceLocationException sle) {
// something went wrong internally, can't reply :(
return null;
}
@@ -198,45 +219,51 @@
// scopes not supported, drop silently
return null;
}// end of directory-agent lookup reply
-
-
- ServiceReplyMessage servreply = new ServiceReplyMessage();
+
+ final ServiceReplyMessage servreply = new ServiceReplyMessage();
servreply.setXid(srvReq.getXid());
servreply.setLocale(srvReq.getLocale());
-
+
if (SLPCore.CONFIG.getSecurityEnabled()) {
boolean knownSpi = false;
- List<String> knownSpis = SLPUtils.stringToList(SLPCore.CONFIG.getSPI(), ",");
- for (String s : srvReq.getSPIs()){
- if (knownSpis.contains(s)){
+ final List<String> knownSpis = SLPUtils.stringToList(
+ SLPCore.CONFIG.getSPI(), ",");
+ for (final String s : srvReq.getSPIs()) {
+ if (knownSpis.contains(s)) {
knownSpi = true;
break;
}
}
- if (!knownSpi){
- servreply.setErrorCode(ServiceLocationException.AUTHENTICATION_UNKNOWN);
+ if (!knownSpi) {
+ servreply
+ .setErrorCode(ServiceLocationException.AUTHENTICATION_UNKNOWN);
return servreply;
}
}
- List<String> overlap = new ArrayList<String>();
- for (String scope : srvReq.getScopes()){
- if (isInScope(scope)){
+ final List<String> overlap = new ArrayList<String>();
+ for (final String scope : srvReq.getScopes()) {
+ if (isInScope(scope)) {
overlap.add(scope);
}
}
- List<ServiceURL> results = new ArrayList<ServiceURL>();
- List<AbstractExtension> extensions = new ArrayList<AbstractExtension>();
- List<Service> services = store.getServices(srvReq.getServiceType().toString(),srvReq.getPredicate(),overlap.toArray(new String[]{}));
- for (Service s : services){
- if (!results.contains(s.getURL())){
+ final List<ServiceURL> results = new ArrayList<ServiceURL>();
+ final List<AbstractExtension> extensions = new ArrayList<AbstractExtension>();
+ final List<Service> services = store.getServices(srvReq
+ .getServiceType().toString(), srvReq.getPredicate(),
+ overlap.toArray(new String[] {}));
+ for (final Service s : services) {
+ if (!results.contains(s.getURL())) {
results.add(s.getURL());
- if (srvReq.hasExtensionType(AbstractExtension.ATTRIBUTE_LIST_EXTENSION)){
- AttributeListExtension ale = new AttributeListExtension(s.getURL().toString(),SLPUtils.dictToString(s.getAttributesAsStringDict()));
+ if (srvReq
+ .hasExtensionType(AbstractExtension.ATTRIBUTE_LIST_EXTENSION)) {
+ final AttributeListExtension ale = new AttributeListExtension(
+ s.getURL().toString(), SLPUtils.dictToString(s
+ .getAttributesAsStringDict()));
extensions.add(ale);
}
}
}
-
+
/*
* if there is no result, don't send a reply. This causes the SA to
* get the same message at least two more times but the RFC strictly
@@ -245,690 +272,787 @@
if (results.size() == 0 && srvReq.isMulticast()) {
return null;
}
-
+
servreply.setErrorCode((short) 0);
- servreply.setServiceURLs(results.toArray(new ServiceURL[]{}));
- servreply.setExtensions(extensions.toArray(new AbstractExtension[]{}));
-
+ servreply.setServiceURLs(results.toArray(new ServiceURL[] {}));
+ servreply.setExtensions(extensions
+ .toArray(new AbstractExtension[] {}));
+
if (SLPCore.CONFIG.getSecurityEnabled()) {
- servreply.sign(SLPUtils.arrayToString(srvReq.getSPIs(),","));
+ servreply.sign(SLPUtils.arrayToString(srvReq.getSPIs(), ","));
}
-
+
return servreply;
-
+
case AbstractSLPMessage.ATTRRQST:
-
- if (msg.hasUnsupportedMandatoryExtensions()){
- AttributeReplyMessage attreply = new AttributeReplyMessage();
+
+ if (msg.hasUnsupportedMandatoryExtensions()) {
+ final AttributeReplyMessage attreply = new AttributeReplyMessage();
attreply.setXid(msg.getXid());
attreply.setLocale(msg.getLocale());
- attreply.setErrorCode(ServiceLocationException.OPTION_NOT_UNDERSTOOD);
+ attreply
+ .setErrorCode(ServiceLocationException.OPTION_NOT_UNDERSTOOD);
return attreply;
}
-
- AttributeRequestMessage attreq = (AttributeRequestMessage) msg;
- if (!scopeOverlap){
+
+ final AttributeRequestMessage attreq = (AttributeRequestMessage) msg;
+ if (!scopeOverlap) {
return null;
}
- //moved this out of the for loop for performance
-// the request can either be for a ServiceURL or a ServiceType
+ // moved this out of the for loop for performance
+ // the request can either be for a ServiceURL or a ServiceType
Object reqService;
boolean fullurl = false;
- if ((attreq.getServiceUrl().getURL().indexOf("//") == -1) || (attreq.getServiceUrl().getHost().equals("nullnull"))) {
+ if (attreq.getServiceUrl().getURL().indexOf("//") == -1
+ || attreq.getServiceUrl().getHost().equals("nullnull")) {
reqService = attreq.getServiceUrl().getServiceType();
} else {
fullurl = true;
reqService = new ServiceURL(attreq.getServiceUrl().getURL(), 0);
}
- String[] attResult = store.getAttributes(reqService.toString(),attreq.getTags(), attreq.getScopes());
- if (!fullurl){
- attResult=SLPUtils.mergeAttributes(attResult);
+ String[] attResult = store.getAttributes(reqService.toString(),
+ attreq.getTags(), attreq.getScopes());
+ if (!fullurl) {
+ attResult = SLPUtils.mergeAttributes(attResult);
}
- AttributeReplyMessage attrep = new AttributeReplyMessage();
+ final AttributeReplyMessage attrep = new AttributeReplyMessage();
attrep.setAttributes(SLPUtils.mergeAttributes(attResult));
attrep.setLocale(attreq.getLocale());
attrep.setXid(attreq.getXid());
attrep.setAuthBlocks(new AuthenticationBlock[0]);
- attrep.setErrorCode((short)0);
- if (attreq.getSPIs().length>0 && !fullurl){
- attrep.setAttributes(new String[]{});
- attrep.setErrorCode(ServiceLocationException.AUTHENTICATION_FAILED);
+ attrep.setErrorCode((short) 0);
+ if (attreq.getSPIs().length > 0 && !fullurl) {
+ attrep.setAttributes(new String[] {});
+ attrep
+ .setErrorCode(ServiceLocationException.AUTHENTICATION_FAILED);
}
-
-
if (SLPCore.CONFIG.getSecurityEnabled()) {
- attrep.sign(SLPUtils.arrayToString(attreq.getSPIs(),","));
+ attrep.sign(SLPUtils.arrayToString(attreq.getSPIs(), ","));
}
return attrep;
case AbstractSLPMessage.SRVTYPERQST:
-
- if (msg.hasUnsupportedMandatoryExtensions()){
- ServiceTypeReplyMessage streply = new ServiceTypeReplyMessage();
+
+ if (msg.hasUnsupportedMandatoryExtensions()) {
+ final ServiceTypeReplyMessage streply = new ServiceTypeReplyMessage();
streply.setXid(msg.getXid());
streply.setLocale(msg.getLocale());
- streply.setErrorCode(ServiceLocationException.OPTION_NOT_UNDERSTOOD);
+ streply
+ .setErrorCode(ServiceLocationException.OPTION_NOT_UNDERSTOOD);
return streply;
}
-
- ServiceTypeRequestMessage streq = (ServiceTypeRequestMessage) msg;
- if (!scopeOverlap){
+
+ final ServiceTypeRequestMessage streq = (ServiceTypeRequestMessage) msg;
+ if (!scopeOverlap) {
return null;
}
- ServiceType[] result = store.getServiceTypes(streq.getScopes(),streq.getNamingAuthority());
-
-
- ServiceTypeReplyMessage streply = new ServiceTypeReplyMessage();
+ final ServiceType[] result = store.getServiceTypes(streq
+ .getScopes(), streq.getNamingAuthority());
+
+ final ServiceTypeReplyMessage streply = new ServiceTypeReplyMessage();
streply.setXid(streq.getXid());
streply.setLocale(streq.getLocale());
- streply.setErrorCode((short)0);
+ streply.setErrorCode((short) 0);
streply.setServiceTypes(result);
return streply;
-
+
// registration and deregistration
case AbstractSLPMessage.SRVREG:
// process the message and check if everything is ok.
// then cache the service.
-
- if (msg.hasUnsupportedMandatoryExtensions()){
- ServiceAcknowledgementMessage ack = new ServiceAcknowledgementMessage();
+
+ if (msg.hasUnsupportedMandatoryExtensions()) {
+ final ServiceAcknowledgementMessage ack = new ServiceAcknowledgementMessage();
ack.setXid(msg.getXid());
ack.setLocale(msg.getLocale());
- ack.setErrorCode(ServiceLocationException.OPTION_NOT_UNDERSTOOD);
+ ack
+ .setErrorCode(ServiceLocationException.OPTION_NOT_UNDERSTOOD);
return ack;
}
-
- ServiceRegistrationMessage reg = (ServiceRegistrationMessage) msg;
-
+
+ final ServiceRegistrationMessage reg = (ServiceRegistrationMessage) msg;
+
ServiceAcknowledgementMessage ack = new ServiceAcknowledgementMessage();
ack.setXid(reg.getXid());
ack.setErrorCode((short) 0);
-
- if (!reg.checkAttributeListValidity()){
+
+ if (!reg.checkAttributeListValidity()) {
ack.setErrorCode(ServiceLocationException.INVALID_REGISTRATION);
return ack;
}
-
- Service service = new Service(reg);
+
+ final Service service = new Service(reg);
boolean found = false;
- for (int i=0;i<reg.getScopes().length;i++) {
+ for (int i = 0; i < reg.getScopes().length; i++) {
String scope = reg.getScopes()[i];
scope = scope.toLowerCase().trim();
-
+
// is the DA configured for this scope?
- if (myScopes.contains(scope)){
+ if (myScopes.contains(scope)) {
found = true;
break;
}
-
+
}
-
- if (!found){
+
+ if (!found) {
// reply with an error if not:
ack.setErrorCode(ServiceLocationException.SCOPE_NOT_SUPPORTED);
return ack;
}
-
+
try {
// is this a fresh registration, or an incremental one?
- boolean exists = isKnownService(service);
- if (exists){
+ final boolean exists = isKnownService(service);
+ if (exists) {
updateServiceEntry(reg);
} else {
registerService(reg);
}
- } catch (ServiceLocationException sle){
+ } catch (final ServiceLocationException sle) {
ack.setErrorCode(sle.getErrorCode());
}
-
+
return ack;
-
-
-
-
-
+
case AbstractSLPMessage.SRVDEREG:
-// process the message and check if everything is ok.
+ // process the message and check if everything is ok.
// then cache the service.
-
- if (msg.hasUnsupportedMandatoryExtensions()){
+
+ if (msg.hasUnsupportedMandatoryExtensions()) {
ack = new ServiceAcknowledgementMessage();
ack.setXid(msg.getXid());
ack.setLocale(msg.getLocale());
- ack.setErrorCode(ServiceLocationException.OPTION_NOT_UNDERSTOOD);
+ ack
+ .setErrorCode(ServiceLocationException.OPTION_NOT_UNDERSTOOD);
return ack;
}
-
- ServiceDeregistrationMessage dereg = (ServiceDeregistrationMessage) msg;
-
+
+ final ServiceDeregistrationMessage dereg = (ServiceDeregistrationMessage) msg;
+
ack = new ServiceAcknowledgementMessage();
ack.setXid(dereg.getXid());
ack.setErrorCode((short) 0);
-
+
found = false;
- for (int i=0;i<dereg.getScopes().length;i++) {
+ for (int i = 0; i < dereg.getScopes().length; i++) {
String scope = dereg.getScopes()[i];
scope = scope.toLowerCase().trim();
-
+
// is the DA configured for this scope?
- if (myScopes.contains(scope)){
+ if (myScopes.contains(scope)) {
found = true;
break;
}
-
+
}
-
- if (!found){
+
+ if (!found) {
// reply with an error if not:
ack.setErrorCode(ServiceLocationException.SCOPE_NOT_SUPPORTED);
return ack;
}
-
+
try {
deregisterService(dereg);
- } catch (ServiceLocationException sle){
+ } catch (final ServiceLocationException sle) {
ack.setErrorCode(sle.getErrorCode());
}
-
+
return ack;
-
+
case AbstractSLPMessage.SRVACK:
- if (msg.hasUnsupportedMandatoryExtensions()){
+ if (msg.hasUnsupportedMandatoryExtensions()) {
return null;
}
final AbstractSLPReplyMessage rep = (AbstractSLPReplyMessage) msg;
if (rep.getErrorCode() != 0) {
SLPCore.platform.logWarning(msg.getSource()
- + " replied with error code " + rep.getErrorCode()
- + " (" + rep + ")");
+ + " replied with error code " + rep.getErrorCode()
+ + " (" + rep + ")");
}
- SLPCore.addReply(msg, new InetSocketAddress(msg.getSource(),msg.getPort()));
- return null;
-
+ SLPCore.addReply(msg, new InetSocketAddress(msg.getSource(), msg
+ .getPort()));
+ return null;
+
default:
-
+
return null;
}
}
-
- /* (non-Javadoc)
- * @see ch.ethz.iks.slp.impl.da.DirectoryAgentDaemon#setStore(ch.ethz.iks.slp.impl.da.ServiceStore)
+ /*
+ * (non-Javadoc)
+ *
+ * @see
+ * ch.ethz.iks.slp.impl.da.DirectoryAgentDaemon#setStore(ch.ethz.iks.slp
+ * .impl.da.ServiceStore)
*/
- public void setStore(ServiceStore store){
- // TODO: should dynamic switchover be allowed? in this case service must be extracted from the
+ public void setStore(final ServiceStore store) {
+ // TODO: should dynamic switchover be allowed? in this case service must
+ // be extracted from the
// old store and injected into the new one...
this.store = store;
}
-
- /* (non-Javadoc)
- * @see ch.ethz.iks.slp.impl.da.DirectoryAgentDaemon#registerService(ch.ethz.iks.slp.messages.ServiceRegistrationMessage)
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see
+ * ch.ethz.iks.slp.impl.da.DirectoryAgentDaemon#registerService(ch.ethz.
+ * iks.slp.messages.ServiceRegistrationMessage)
*/
- public void registerService(ServiceRegistrationMessage reg) throws ServiceLocationException{
+ public void registerService(final ServiceRegistrationMessage reg)
+ throws ServiceLocationException {
// test for incremental registration has been done
- if (SLPCore.CONFIG.getSecurityEnabled()){
+ if (SLPCore.CONFIG.getSecurityEnabled()) {
// message has been authenticated
// dump all auth blocks that are not understood
- List<AuthenticationBlock> auth = new ArrayList<AuthenticationBlock>();
- List<String> mySpis = SLPUtils.stringToList(SLPCore.CONFIG.getSPI(),",");
- for (AuthenticationBlock b : reg.getAuthBlocks()){
- if (mySpis.contains(b.getSpi())){
+ final List<AuthenticationBlock> auth = new ArrayList<AuthenticationBlock>();
+ final List<String> mySpis = SLPUtils.stringToList(SLPCore.CONFIG
+ .getSPI(), ",");
+ for (final AuthenticationBlock b : reg.getAuthBlocks()) {
+ if (mySpis.contains(b.getSpi())) {
auth.add(b);
}
}
- reg.setAuthBlocks(auth.toArray(new AuthenticationBlock[]{}));
+ reg.setAuthBlocks(auth.toArray(new AuthenticationBlock[] {}));
+ }
+
+ // first check if FRESH is NOT set but no service has been registered to
+ // update...
+ if (!reg.isFresh()) {
+ throw new ServiceLocationException(
+ ServiceLocationException.INVALID_UPDATE,
+ "Tried to register a new service without the FRESH flag set");
}
-
-
-
- // first check if FRESH is NOT set but no service has been registered to update...
- if (!reg.isFresh()){
- throw new ServiceLocationException(ServiceLocationException.INVALID_UPDATE,"Tried to register a new service without the FRESH flag set");
- }
-
-
- Service service = new Service(reg);
-
-
+
+ final Service service = new Service(reg);
+
store.storeService(service);
-
+
if (reg.getServiceURL().getLifetime() > ServiceURL.LIFETIME_PERMANENT) {
synchronized (serviceDisposalQueue) {
- long next = System.currentTimeMillis()
- + (reg.getServiceURL().getLifetime() * 1000);
- ArrayList<Long> keys = new ArrayList<Long>(serviceDisposalQueue
- .keySet());
- for (Iterator iter = keys.iterator(); iter.hasNext();) {
- Object key = iter.next();
- if (serviceDisposalQueue.get(key).equals(reg.getServiceURL().toString())) {
+ final long next = System.currentTimeMillis()
+ + reg.getServiceURL().getLifetime() * 1000;
+ final ArrayList<Long> keys = new ArrayList<Long>(
+ serviceDisposalQueue.keySet());
+ for (final Iterator iter = keys.iterator(); iter.hasNext();) {
+ final Object key = iter.next();
+ if (serviceDisposalQueue.get(key).equals(
+ reg.getServiceURL().toString())) {
serviceDisposalQueue.remove(key);
}
}
- serviceDisposalQueue.put(new Long(next), reg.getServiceURL().toString());
+ serviceDisposalQueue.put(new Long(next), reg.getServiceURL()
+ .toString());
serviceDisposalQueue.notifyAll();
}
}
SLPCore.platform.logTraceReg("REGISTERED " + reg.getServiceURL());
- // TODO: pass on registration to fellow DAs ???
+ // TODO: pass on registration to fellow DAs ???
}
-
-
- /* (non-Javadoc)
- * @see ch.ethz.iks.slp.impl.da.DirectoryAgentDaemon#registerService(ch.ethz.iks.slp.impl.Service)
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see
+ * ch.ethz.iks.slp.impl.da.DirectoryAgentDaemon#registerService(ch.ethz.
+ * iks.slp.impl.Service)
*/
- public void registerService(Service service) throws ServiceLocationException{
+ public void registerService(final Service service)
+ throws ServiceLocationException {
// this call should only be made through the DirectoryAgent interface
// no checks are made
-
+
store.storeService(service);
-
+
if (service.getURL().getLifetime() > ServiceURL.LIFETIME_PERMANENT) {
synchronized (serviceDisposalQueue) {
- long next = System.currentTimeMillis()
- + (service.getURL().getLifetime() * 1000);
- ArrayList<Long> keys = new ArrayList<Long>(serviceDisposalQueue
- .keySet());
- for (Iterator iter = keys.iterator(); iter.hasNext();) {
- Object key = iter.next();
- if (serviceDisposalQueue.get(key).equals(service.getURL().toString())) {
+ final long next = System.currentTimeMillis()
+ + service.getURL().getLifetime() * 1000;
+ final ArrayList<Long> keys = new ArrayList<Long>(
+ serviceDisposalQueue.keySet());
+ for (final Iterator iter = keys.iterator(); iter.hasNext();) {
+ final Object key = iter.next();
+ if (serviceDisposalQueue.get(key).equals(
+ service.getURL().toString())) {
serviceDisposalQueue.remove(key);
}
}
- serviceDisposalQueue.put(new Long(next), service.getURL().toString());
+ serviceDisposalQueue.put(new Long(next), service.getURL()
+ .toString());
serviceDisposalQueue.notifyAll();
}
}
SLPCore.platform.logTraceReg("REGISTERED " + service.getURL());
- // TODO: pass on registration to fellow DAs ???
+ // TODO: pass on registration to fellow DAs ???
}
-
-
- /* (non-Javadoc)
- * @see ch.ethz.iks.slp.impl.da.DirectoryAgentDaemon#deregisterService(ch.ethz.iks.slp.messages.ServiceDeregistrationMessage)
- */
- public void deregisterService(ServiceDeregistrationMessage dereg) throws ServiceLocationException{
-
- Service registeredService = store.getService(dereg.getServiceURL().toString());
-
- if (registeredService==null){
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see
+ * ch.ethz.iks.slp.impl.da.DirectoryAgentDaemon#deregisterService(ch.ethz
+ * .iks.slp.messages.ServiceDeregistrationMessage)
+ */
+ public void deregisterService(final ServiceDeregistrationMessage dereg)
+ throws ServiceLocationException {
+
+ final Service registeredService = store.getService(dereg
+ .getServiceURL().toString());
+
+ if (registeredService == null) {
// no specs for this case given in RFC2608...
- throw new ServiceLocationException(ServiceLocationException.INVALID_REGISTRATION,"No such Service registered");
+ throw new ServiceLocationException(
+ ServiceLocationException.INVALID_REGISTRATION,
+ "No such Service registered");
+ }
+
+ // check if the <scope-list> matches the one used in previous
+ // registration
+ final List<String> previousScopes = registeredService.getScopes();
+ if (previousScopes.size() != dereg.getScopes().length) {
+ throw new ServiceLocationException(
+ ServiceLocationException.SCOPE_NOT_SUPPORTED,
+ "Tried to deregister an existing entry registered with a different scope-list");
+ }
+ for (final String scope : dereg.getScopes()) {
+ if (!previousScopes.contains(scope.toLowerCase().trim())) {
+ throw new ServiceLocationException(
+ ServiceLocationException.SCOPE_NOT_SUPPORTED,
+ "Tried to deregister an existing entry registered with a different scope-list");
+ }
}
-
- //check if the <scope-list> matches the one used in previous registration
- List<String> previousScopes = registeredService.getScopes();
- if (previousScopes.size()!=dereg.getScopes().length){
- throw new ServiceLocationException(ServiceLocationException.SCOPE_NOT_SUPPORTED, "Tried to deregister an existing entry registered with a different scope-list");
- }
- for (String scope: dereg.getScopes()){
- if (!previousScopes.contains(scope.toLowerCase().trim())){
- throw new ServiceLocationException(ServiceLocationException.SCOPE_NOT_SUPPORTED, "Tried to deregister an existing entry registered with a different scope-list");
- }
- }
-
- // check if tag-list is non-zero, in which case only the attributes in the tag list are deleted
- if (dereg.getTags().length>0){
- if (registeredService.getAuthBlocks().length>0){
- throw new ServiceLocationException(ServiceLocationException.AUTHENTICATION_FAILED, "Tried to deregister attributes of an existing entry registered with authentication blocks");
- }
-
- Dictionary<String,String> attributes = registeredService.getAttributesAsStringDict();
- // find all matching attributes in the services attribute dictionary...
- List<String> matches = SLPUtils.findMatches(SLPUtils.arrayToList(dereg.getTags()), attributes);
+
+ // check if tag-list is non-zero, in which case only the attributes in
+ // the tag list are deleted
+ if (dereg.getTags().length > 0) {
+ if (registeredService.getAuthBlocks().length > 0) {
+ throw new ServiceLocationException(
+ ServiceLocationException.AUTHENTICATION_FAILED,
+ "Tried to deregister attributes of an existing entry registered with authentication blocks");
+ }
+
+ final Dictionary<String, String> attributes = registeredService
+ .getAttributesAsStringDict();
+ // find all matching attributes in the services attribute
+ // dictionary...
+ final List<String> matches = SLPUtils.findMatches(SLPUtils
+ .arrayToList(dereg.getTags()), attributes);
// and extract the attribute types...
- for (String s : matches){
+ for (final String s : matches) {
// s is of form "(ATTRIBUTE)" or "(ATTRIBUTE=VALUE)"
- int end = s.length()-1;
- if (s.indexOf("=")>-1){
+ int end = s.length() - 1;
+ if (s.indexOf("=") > -1) {
end = s.indexOf("=");
}
- String q = s.substring(1,end);
+ final String q = s.substring(1, end);
registeredService.getAttributes().remove(q);
}
-
-
-
+
store.deleteService(dereg.getServiceURL().toString());
store.storeService(registeredService);
- SLPCore.platform.logTraceReg("DEREGISTERED ATTRIBUTES ["+dereg.getTags()+"] from " + dereg.getServiceURL());
+ SLPCore.platform.logTraceReg("DEREGISTERED ATTRIBUTES ["
+ + dereg.getTags() + "] from " + dereg.getServiceURL());
return;
}
-
-
- if (registeredService.getURL().getAuthBlocks().length>0){
+
+ if (registeredService.getURL().getAuthBlocks().length > 0) {
// unsure about what RFC 2608 means in the part concerning this...
-
+
// fetch all spis for this reg message
- AuthenticationBlock[] authBlocks = dereg.getServiceURL().getAuthBlocks();
- String[] spis = new String[authBlocks.length];
- for (int i = 0; i< authBlocks.length; i++){
+ final AuthenticationBlock[] authBlocks = dereg.getServiceURL()
+ .getAuthBlocks();
+ final String[] spis = new String[authBlocks.length];
+ for (int i = 0; i < authBlocks.length; i++) {
spis[i] = authBlocks[i].getSpi();
}
// now get the spis of the stored service
- AuthenticationBlock[] registeredAuthBlocks = registeredService.getURL().getAuthBlocks();
- String[] registeredSpis = new String[registeredAuthBlocks.length];
- for (int i = 0; i< registeredAuthBlocks.length; i++){
+ final AuthenticationBlock[] registeredAuthBlocks = registeredService
+ .getURL().getAuthBlocks();
+ final String[] registeredSpis = new String[registeredAuthBlocks.length];
+ for (int i = 0; i < registeredAuthBlocks.length; i++) {
registeredSpis[i] = registeredAuthBlocks[i].getSpi();
}
- if (registeredSpis.length!=spis.length){
- throw new ServiceLocationException(ServiceLocationException.AUTHENTICATION_FAILED,"SPIs did not match existing entry");
+ if (registeredSpis.length != spis.length) {
+ throw new ServiceLocationException(
+ ServiceLocationException.AUTHENTICATION_FAILED,
+ "SPIs did not match existing entry");
}
- for (int i = 0;i<spis.length;i++){
+ for (int i = 0; i < spis.length; i++) {
boolean found = false;
// yes, this runs in O(n²), may be fixed later if possible
- for (int j=0;j<registeredSpis.length;j++){
- if (spis[i].equals(registeredSpis[j])){
- found=true;
+ for (int j = 0; j < registeredSpis.length; j++) {
+ if (spis[i].equals(registeredSpis[j])) {
+ found = true;
}
}
- if (!found){
- throw new ServiceLocationException(ServiceLocationException.AUTHENTICATION_FAILED,"SPIs did not match existing entry");
+ if (!found) {
+ throw new ServiceLocationException(
+ ServiceLocationException.AUTHENTICATION_FAILED,
+ "SPIs did not match existing entry");
}
}
-
+
} // end of security enabled block
-
+
store.deleteService(dereg.getServiceURL().toString());
-
+
serviceDisposalQueue.values().remove(dereg.getServiceURL().toString());
-
SLPCore.platform.logTraceReg("DEREGISTERED " + dereg.getServiceURL());
- // TODO: pass on deregistration to fellow DAs ???
+ // TODO: pass on deregistration to fellow DAs ???
}
-
-
-
-
- /* (non-Javadoc)
- * @see ch.ethz.iks.slp.impl.da.DirectoryAgentDaemon#deregisterService(ch.ethz.iks.slp.impl.Service)
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see
+ * ch.ethz.iks.slp.impl.da.DirectoryAgentDaemon#deregisterService(ch.ethz
+ * .iks.slp.impl.Service)
*/
- public void deregisterService(Service service) throws ServiceLocationException{
-
+ public void deregisterService(final Service service)
+ throws ServiceLocationException {
+
store.deleteService(service.getURL().toString());
-
+
serviceDisposalQueue.values().remove(service.getURL().toString());
-
+
SLPCore.platform.logTraceReg("DEREGISTERED " + service.getURL());
- // TODO: pass on deregistration to fellow DAs ???
+ // TODO: pass on deregistration to fellow DAs ???
}
-
-
-
-
- /* (non-Javadoc)
- * @see ch.ethz.iks.slp.impl.da.DirectoryAgentDaemon#updateServiceEntry(ch.ethz.iks.slp.messages.ServiceRegistrationMessage)
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see
+ * ch.ethz.iks.slp.impl.da.DirectoryAgentDaemon#updateServiceEntry(ch.ethz
+ * .iks.slp.messages.ServiceRegistrationMessage)
*/
- public void updateServiceEntry(ServiceRegistrationMessage reg) throws ServiceLocationException{
+ public void updateServiceEntry(final ServiceRegistrationMessage reg)
+ throws ServiceLocationException {
// this is an update, so let's first fetch the existing service
- Service registeredService = store.getService(reg.getServiceURL().toString());
-
+ final Service registeredService = store.getService(reg.getServiceURL()
+ .toString());
+
// if the resh flag is set, overwrite the existing entry
- if (reg.isFresh()){
+ if (reg.isFresh()) {
try {
-// check if the <scope-list> matches the one used in previous registration
- List<String> previousScopes = registeredService.getScopes();
- if (previousScopes.size()!=reg.getScopes().length){
- throw new ServiceLocationException(ServiceLocationException.SCOPE_NOT_SUPPORTED, "Tried to update an existing entry registered with a different scope-list");
- }
- for (String scope: reg.getScopes()){
- if (!previousScopes.contains(scope.toLowerCase().trim())){
- throw new ServiceLocationException(ServiceLocationException.SCOPE_NOT_SUPPORTED, "Tried to update an existing entry registered with a different scope-list");
+ // check if the <scope-list> matches the one used in previous
+ // registration
+ final List<String> previousScopes = registeredService
+ .getScopes();
+ if (previousScopes.size() != reg.getScopes().length) {
+ throw new ServiceLocationException(
+ ServiceLocationException.SCOPE_NOT_SUPPORTED,
+ "Tried to update an existing entry registered with a different scope-list");
+ }
+ for (final String scope : reg.getScopes()) {
+ if (!previousScopes.contains(scope.toLowerCase().trim())) {
+ throw new ServiceLocationException(
+ ServiceLocationException.SCOPE_NOT_SUPPORTED,
+ "Tried to update an existing entry registered with a different scope-list");
}
}
- // check if the slp spis are identical to the ones previously used
- if (SLPCore.CONFIG.getSecurityEnabled()){
+ // check if the slp spis are identical to the ones previously
+ // used
+ if (SLPCore.CONFIG.getSecurityEnabled()) {
// fetch all spis for this reg message
- AuthenticationBlock[] authBlocks = reg.getAuthBlocks();
- String[] spis = new String[authBlocks.length];
- for (int i = 0; i< authBlocks.length; i++){
+ final AuthenticationBlock[] authBlocks = reg
+ .getAuthBlocks();
+ final String[] spis = new String[authBlocks.length];
+ for (int i = 0; i < authBlocks.length; i++) {
spis[i] = authBlocks[i].getSpi();
}
// now get the spis of the stored service
- AuthenticationBlock[] registeredAuthBlocks = registeredService.getAuthBlocks();
- String[] registeredSpis = new String[registeredAuthBlocks.length];
- for (int i = 0; i< registeredAuthBlocks.length; i++){
+ final AuthenticationBlock[] registeredAuthBlocks = registeredService
+ .getAuthBlocks();
+ final String[] registeredSpis = new String[registeredAuthBlocks.length];
+ for (int i = 0; i < registeredAuthBlocks.length; i++) {
registeredSpis[i] = registeredAuthBlocks[i].getSpi();
}
- if (registeredSpis.length!=spis.length){
- throw new ServiceLocationException(ServiceLocationException.INVALID_UPDATE,"SPIs did not match existing entry");
+ if (registeredSpis.length != spis.length) {
+ throw new ServiceLocationException(
+ ServiceLocationException.INVALID_UPDATE,
+ "SPIs did not match existing entry");
}
- for (int i = 0;i<spis.length;i++){
+ for (int i = 0; i < spis.length; i++) {
boolean found = false;
- // yes, this runs in O(n²), may be fixed later if possible
- for (int j=0;j<registeredSpis.length;j++){
- if (spis[i].equals(registeredSpis[j])){
- found=true;
+ // yes, this runs in O(n²), may be fixed later if
+ // possible
+ for (int j = 0; j < registeredSpis.length; j++) {
+ if (spis[i].equals(registeredSpis[j])) {
+ found = true;
}
}
- if (!found){
- throw new ServiceLocationException(ServiceLocationException.AUTHENTICATION_ABSENT,"SPIs did not match existing entry");
+ if (!found) {
+ throw new ServiceLocationException(
+ ServiceLocationException.AUTHENTICATION_ABSENT,
+ "SPIs did not match existing entry");
}
}
-
+
}
-
+
// scope checks out, security checks out, replace it
-
+
store.deleteService(reg.getServiceURL().toString());
store.storeService(new Service(reg));
- SLPCore.platform.logTraceReg("REREGISTERED " + reg.getServiceURL());
+ SLPCore.platform.logTraceReg("REREGISTERED "
+ + reg.getServiceURL());
return;
- } catch (ServiceLocationException sle){
- throw new ServiceLocationException(ServiceLocationException.INTERNAL_SYSTEM_ERROR,"Problems updating service "+reg.getServiceURL());
+ } catch (final ServiceLocationException sle) {
+ throw new ServiceLocationException(
+ ServiceLocationException.INTERNAL_SYSTEM_ERROR,
+ "Problems updating service " + reg.getServiceURL());
}
} else {
// incremental update required
-
+
// first: check if there even exists a previous entry
- if (registeredService==null){
- throw new ServiceLocationException(ServiceLocationException.INVALID_UPDATE, "Tried to update a non-existing entry");
+ if (registeredService == null) {
+ throw new ServiceLocationException(
+ ServiceLocationException.INVALID_UPDATE,
+ "Tried to update a non-existing entry");
+ }
+
+ // second: check if the service was previously registered with auth
+ // blocks
+ if (registeredService.getAuthBlocks().length > 0) {
+ throw new ServiceLocationException(
+ ServiceLocationException.AUTHENTICATION_FAILED,
+ "Tried to update an existing entry registered with AuthBlocks");
+ }
+
+ // third: check if the <scope-list> matches the one used in previous
+ // registration
+ final List<String> previousScopes = registeredService.getScopes();
+ if (previousScopes.size() != reg.getScopes().length) {
+ throw new ServiceLocationException(
+ ServiceLocationException.SCOPE_NOT_SUPPORTED,
+ "Tried to update an existing entry registered with a different scope-list");
+ }
+ for (final String scope : reg.getScopes()) {
+ if (!previousScopes.contains(scope.toLowerCase().trim())) {
+ throw new ServiceLocationException(
+ ServiceLocationException.SCOPE_NOT_SUPPORTED,
+ "Tried to update an existing entry registered with a different scope-list");
+ }
+ }
+
+ // fourth: check if the service-type is the same as previously
+ // registered
+ if (!registeredService.getURL().getServiceType().equals(
+ reg.getServiceType())) {
+ throw new ServiceLocationException(
+ ServiceLocationException.INVALID_UPDATE,
+ "Tried to update an existing entry registered with a different service type");
}
-
- // second: check if the service was previously registered with auth blocks
- if (registeredService.getAuthBlocks().length > 0){
- throw new ServiceLocationException(ServiceLocationException.AUTHENTICATION_FAILED, "Tried to update an existing entry registered with AuthBlocks");
- }
-
- // third: check if the <scope-list> matches the one used in previous registration
- List<String> previousScopes = registeredService.getScopes();
- if (previousScopes.size()!=reg.getScopes().length){
- throw new ServiceLocationException(ServiceLocationException.SCOPE_NOT_SUPPORTED, "Tried to update an existing entry registered with a different scope-list");
- }
- for (String scope: reg.getScopes()){
- if (!previousScopes.contains(scope.toLowerCase().trim())){
- throw new ServiceLocationException(ServiceLocationException.SCOPE_NOT_SUPPORTED, "Tried to update an existing entry registered with a different scope-list");
- }
- }
-
- // fourth: check if the service-type is the same as previously registered
- if (!registeredService.getURL().getServiceType().equals(reg.getServiceType())){
- throw new ServiceLocationException(ServiceLocationException.INVALID_UPDATE, "Tried to update an existing entry registered with a different service type");
- }
-
- // finally, check if the slp spis are identical to the ones previously used
- if (SLPCore.CONFIG.getSecurityEnabled()){
+
+ // finally, check if the slp spis are identical to the ones
+ // previously used
+ if (SLPCore.CONFIG.getSecurityEnabled()) {
// fetch all spis for this reg message
- AuthenticationBlock[] authBlocks = reg.getAuthBlocks();
- String[] spis = new String[authBlocks.length];
- for (int i = 0; i< authBlocks.length; i++){
+ final AuthenticationBlock[] authBlocks = reg.getAuthBlocks();
+ final String[] spis = new String[authBlocks.length];
+ for (int i = 0; i < authBlocks.length; i++) {
spis[i] = authBlocks[i].getSpi();
}
// now get the spis of the stored service
- AuthenticationBlock[] registeredAuthBlocks = registeredService.getAuthBlocks();
- String[] registeredSpis = new String[registeredAuthBlocks.length];
- for (int i = 0; i< registeredAuthBlocks.length; i++){
+ final AuthenticationBlock[] registeredAuthBlocks = registeredService
+ .getAuthBlocks();
+ final String[] registeredSpis = new String[registeredAuthBlocks.length];
+ for (int i = 0; i < registeredAuthBlocks.length; i++) {
registeredSpis[i] = registeredAuthBlocks[i].getSpi();
}
- if (registeredSpis.length!=spis.length){
- throw new ServiceLocationException(ServiceLocationException.INVALID_UPDATE,"SPIs did not match existing entry");
+ if (registeredSpis.length != spis.length) {
+ throw new ServiceLocationException(
+ ServiceLocationException.INVALID_UPDATE,
+ "SPIs did not match existing entry");
}
- for (int i = 0;i<spis.length;i++){
+ for (int i = 0; i < spis.length; i++) {
boolean found = false;
// yes, this runs in O(n²), may be fixed later if possible
- for (int j=0;j<registeredSpis.length;j++){
- if (spis[i].equals(registeredSpis[j])){
- found=true;
+ for (int j = 0; j < registeredSpis.length; j++) {
+ if (spis[i].equals(registeredSpis[j])) {
+ found = true;
}
}
- if (!found){
- throw new ServiceLocationException(ServiceLocationException.AUTHENTICATION_ABSENT,"SPIs did not match existing entry");
+ if (!found) {
+ throw new ServiceLocationException(
+ ServiceLocationException.AUTHENTICATION_ABSENT,
+ "SPIs did not match existing entry");
}
}
-
+
} // end of security enabled block
-
-
- Dictionary<String,Object> registeredAttributes = registeredService.getAttributes();
- Dictionary<String,Object> newAttributes = SLPUtils.stringArrayToDict(reg.getAttrList());
- Enumeration<String> keys = newAttributes.keys();
- while (keys.hasMoreElements()){
- String key = keys.nextElement();
+ final Dictionary<String, Object> registeredAttributes = registeredService
+ .getAttributes();
+ final Dictionary<String, Object> newAttributes = SLPUtils
+ .stringArrayToDict(reg.getAttrList());
+ final Enumeration<String> keys = newAttributes.keys();
+ while (keys.hasMoreElements()) {
+ final String key = keys.nextElement();
registeredAttributes.remove(key);
registeredAttributes.put(key, newAttributes.get(key));
}
registeredService.setAttributes(registeredAttributes);
} // end of incremental update
-
- // process the new lifetime
+
+ // process the new lifetime
if (reg.getServiceURL().getLifetime() > ServiceURL.LIFETIME_PERMANENT) {
synchronized (serviceDisposalQueue) {
- long next = System.currentTimeMillis()
- + (reg.getServiceURL().getLifetime() * 1000);
- ArrayList<Long> disposalKeys = new ArrayList<Long>(serviceDisposalQueue.keySet());
- for (Long k:disposalKeys) {
- if (serviceDisposalQueue.get(k).equals(reg.getServiceURL().toString())) {
+ final long next = System.currentTimeMillis()
+ + reg.getServiceURL().getLifetime() * 1000;
+ final ArrayList<Long> disposalKeys = new ArrayList<Long>(
+ serviceDisposalQueue.keySet());
+ for (final Long k : disposalKeys) {
+ if (serviceDisposalQueue.get(k).equals(
+ reg.getServiceURL().toString())) {
serviceDisposalQueue.remove(k);
}
}
- serviceDisposalQueue.put(new Long(next), reg.getServiceURL().toString());
+ serviceDisposalQueue.put(new Long(next), reg.getServiceURL()
+ .toString());
serviceDisposalQueue.notifyAll();
}
}
store.deleteService(reg.getServiceURL().toString());
store.storeService(registeredService);
- SLPCore.platform.logTraceReg("REGISTERED INCREMENTALLY " + reg.getServiceURL());
+ SLPCore.platform.logTraceReg("REGISTERED INCREMENTALLY "
+ + reg.getServiceURL());
return;
}
-
- /* (non-Javadoc)
+ /*
+ * (non-Javadoc)
+ *
* @see ch.ethz.iks.slp.impl.da.DirectoryAgentDaemon#buildAdvert()
*/
- public DAAdvertisementMessage buildAdvert() throws ServiceLocationException{
- DAAdvertisementMessage advert = new DAAdvertisementMessage();
+ public DAAdvertisementMessage buildAdvert() throws ServiceLocationException {
+ final DAAdvertisementMessage advert = new DAAdvertisementMessage();
// always return 0... what errors have to be handled here?
advert.setErrorCode((short) 0);
advert.setXid((short) 0);
advert.setStatelessBootTimestamp(statelessBootTimestamp);
advert.setAttributes(attributes);
- //TODO: add min-refresh-interval here
- ServiceURL myService = new ServiceURL("service:directory-agent://"+SLPCore.getMyIP().getHostAddress(),0);
+ // TODO: add min-refresh-interval here
+ final ServiceURL myService = new ServiceURL(
+ "service:directory-agent://"
+ + SLPCore.getMyIP().getHostAddress(), 0);
advert.setServiceURL(myService);
- advert.setOrigURL("service:directory-agent://"+SLPCore.getMyIP().getHostAddress());
- advert.setScopes(SLPUtils.stringToStringArray(SLPCore.CONFIG.getScopes(), ","));
-
+ advert.setOrigURL("service:directory-agent://"
+ + SLPCore.getMyIP().getHostAddress());
+ advert.setScopes(SLPUtils.stringToStringArray(SLPCore.CONFIG
+ .getScopes(), ","));
+
return advert;
-
+
}
-
- /* (non-Javadoc)
- * @see ch.ethz.iks.slp.impl.da.DirectoryAgentDaemon#getStatelessBootTimestamp()
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see
+ * ch.ethz.iks.slp.impl.da.DirectoryAgentDaemon#getStatelessBootTimestamp()
*/
- public int getStatelessBootTimestamp(){
+ public int getStatelessBootTimestamp() {
return statelessBootTimestamp;
}
-
-
- /* (non-Javadoc)
+
+ /*
+ * (non-Javadoc)
+ *
* @see ch.ethz.iks.slp.impl.da.DirectoryAgentDaemon#shutdown()
*/
- public void shutdown(){
+ public void shutdown() {
try {
- if (running){
+ if (running) {
running = false;
- SLPCore.platform.logDebug("jSLP Directory Agent shutting down...");
- DAAdvertisementMessage advert = buildAdvert();
+ SLPCore.platform
+ .logDebug("jSLP Directory Agent shutting down...");
+ final DAAdvertisementMessage advert = buildAdvert();
advert.setStatelessBootTimestamp(0);
- SLPCore.sendUnicastMessage(advert, new InetSocketAddress(SLPCore.MCAST_ADDRESS,SLP_PORT), false);
+ SLPCore.sendUnicastMessage(advert, new InetSocketAddress(
+ SLPCore.MCAST_ADDRESS, SLP_PORT), false);
SLPCore.shutdownDirectoryAgent();
- //store.clear();
+ // store.clear();
}
-
- } catch (ServiceLocationException sle){
+
+ } catch (final ServiceLocationException sle) {
SLPCore.platform.logError("Error while closing down DA", sle);
}
}
-
-
- /* (non-Javadoc)
+
+ /*
+ * (non-Javadoc)
+ *
* @see ch.ethz.iks.slp.impl.da.DirectoryAgentDaemon#getAttributeTypes()
*/
- public Map<String, Integer> getAttributeTypes() throws ServiceLocationException{
+ public Map<String, Integer> getAttributeTypes()
+ throws ServiceLocationException {
return store.getAttributeTypes();
}
-
-
+
/**
- * This removes all unsupported scopes from the list of scopes in a message. After processing a message
- * the scopelist in the message includes only those scopes the DA also supports.
+ * This removes all unsupported scopes from the list of scopes in a message.
+ * After processing a message the scopelist in the message includes only
+ * those scopes the DA also supports.
*
* @param msg
- * An AbstractSLPMessage, such as a ServiceRegistrationMessage
- * @return
- * True if the list of supported scopes still contains an entry, false if none of the scopes
- * listed in the message are supported by the DA.
- */
- private boolean stripUnsupportedScopes(AbstractSLPMessage msg){
- if (msg instanceof ServiceRegistrationMessage){
- ServiceRegistrationMessage reg = (ServiceRegistrationMessage) msg;
- List<String> supportedScopes = new ArrayList<String>();
- for (String s: reg.getScopes()){
- if (myScopes.contains(s.toLowerCase().trim())){
+ * An AbstractSLPMessage, such as a ServiceRegistrationMessage
+ * @return True if the list of supported scopes still contains an entry,
+ * false if none of the scopes listed in the message are supported
+ * by the DA.
+ */
+ private boolean stripUnsupportedScopes(final AbstractSLPMessage msg) {
+ if (msg instanceof ServiceRegistrationMessage) {
+ final ServiceRegistrationMessage reg = (ServiceRegistrationMessage) msg;
+ final List<String> supportedScopes = new ArrayList<String>();
+ for (final String s : reg.getScopes()) {
+ if (myScopes.contains(s.toLowerCase().trim())) {
supportedScopes.add(s.toLowerCase().trim());
}
}
reg.setScopes(SLPUtils.listToStringArray(supportedScopes));
- return (supportedScopes.size()>0);
+ return supportedScopes.size() > 0;
}
- if (msg instanceof ServiceDeregistrationMessage){
- ServiceDeregistrationMessage reg = (ServiceDeregistrationMessage) msg;
- List<String> supportedScopes = new ArrayList<String>();
- for (String s: reg.getScopes()){
- if (myScopes.contains(s.toLowerCase().trim())){
+ if (msg instanceof ServiceDeregistrationMessage) {
+ final ServiceDeregistrationMessage reg = (ServiceDeregistrationMessage) msg;
+ final List<String> supportedScopes = new ArrayList<String>();
+ for (final String s : reg.getScopes()) {
+ if (myScopes.contains(s.toLowerCase().trim())) {
supportedScopes.add(s.toLowerCase().trim());
}
}
reg.setScopes(SLPUtils.listToStringArray(supportedScopes));
- return (supportedScopes.size()>0);
+ return supportedScopes.size() > 0;
}
-
- if (msg instanceof AbstractSLPRequestMessage){
- AbstractSLPRequestMessage req = (AbstractSLPRequestMessage) msg;
- List<String> supportedScopes = new ArrayList<String>();
- for (String s: req.getScopes()){
- if (myScopes.contains(s.toLowerCase().trim())){
+
+ if (msg instanceof AbstractSLPRequestMessage) {
+ final AbstractSLPRequestMessage req = (AbstractSLPRequestMessage) msg;
+ final List<String> supportedScopes = new ArrayList<String>();
+ for (final String s : req.getScopes()) {
+ if (myScopes.contains(s.toLowerCase().trim())) {
supportedScopes.add(s.toLowerCase().trim());
}
}
req.setScopes(SLPUtils.listToStringArray(supportedScopes));
- return (supportedScopes.size()>0);
+ return supportedScopes.size() > 0;
}
return true;
}
-
-
-
/**
* service disposal thread. Removes services from the local registry when
* their lifetime has expired.
@@ -947,6 +1071,7 @@
/**
* thread's main loop.
*/
+ @Override
public void run() {
try {
while (running) {
@@ -954,29 +1079,28 @@
if (serviceDisposalQueue.isEmpty()) {
// nothing to do, sleep until something arrives
SLPCore.platform
- .logDebug("ServiceDisposalThread sleeping ...");
+ .logDebug("ServiceDisposalThread sleeping ...");
serviceDisposalQueue.wait();
} else {
// we have work, do everything that is due
Long nextActivity;
while (!serviceDisposalQueue.isEmpty()
- && (nextActivity = ((Long) serviceDisposalQueue
- .firstKey())).longValue() <= System
+ && (nextActivity = serviceDisposalQueue
+ .firstKey()).longValue() <= System
.currentTimeMillis()) {
-
- String service = serviceDisposalQueue
+
+ final String service = serviceDisposalQueue
.get(nextActivity);
-
+
try {
store.deleteService(service);
- } catch (ServiceLocationException sle) {
- SLPCore.platform.logError(sle
- .getMessage(), sle
- .fillInStackTrace());
+ } catch (final ServiceLocationException sle) {
+ SLPCore.platform.logError(sle.getMessage(),
+ sle.fillInStackTrace());
}
SLPCore.platform
- .logTraceReg("disposed service "
- + service);
+ .logTraceReg("disposed service "
+ + service);
serviceDisposalQueue.remove(nextActivity);
}
if (!serviceDisposalQueue.isEmpty()) {
@@ -984,35 +1108,32 @@
* there are some activities in the future,
* sleep until the first activity becomes due
*/
- nextActivity = ((Long) serviceDisposalQueue
- .firstKey());
- long waitTime = nextActivity.longValue()
+ nextActivity = serviceDisposalQueue.firstKey();
+ final long waitTime = nextActivity.longValue()
- System.currentTimeMillis();
if (waitTime > 0) {
- SLPCore.platform
- .logDebug("sleeping for "
- + waitTime / 1000
- + " seconds.");
+ SLPCore.platform.logDebug("sleeping for "
+ + waitTime / 1000 + " seconds.");
serviceDisposalQueue.wait(waitTime);
}
}
}
}
}
- } catch (InterruptedException ie) {
+ } catch (final InterruptedException ie) {
// let the thread stop.
}
}
}
-
-
+
/**
- * thread for unsolicited DAAdverts. This thread periodically sends out DAAdvertisements
+ * thread for unsolicited DAAdverts. This thread periodically sends out
+ * DAAdvertisements
*/
private final class UnsolicitedAdvertisementThread extends Thread {
private long lastUnsolicitedAdvertisement = 0;
-
+
/**
* create and start a new instance of this thread.
*
@@ -1025,41 +1146,51 @@
/**
* thread's main loop.
*/
+ @Override
public void run() {
long remaining;
- while (running){
+ while (running) {
try {
- if ((remaining=SLPCore.CONFIG.getConfigDABeat()-System.currentTimeMillis()+lastUnsolicitedAdvertisement)<0){
- DAAdvertisementMessage advert = buildAdvert();
- if (SLPCore.CONFIG.getSecurityEnabled()){
- advert.setSPIs(SLPUtils.stringToStringArray(SLPCore.CONFIG.getSPI(),","));
+ if ((remaining = SLPCore.CONFIG.getConfigDABeat()
+ - System.currentTimeMillis()
+ + lastUnsolicitedAdvertisement) < 0) {
+ final DAAdvertisementMessage advert = buildAdvert();
+ if (SLPCore.CONFIG.getSecurityEnabled()) {
+ advert.setSPIs(SLPUtils.stringToStringArray(
+ SLPCore.CONFIG.getSPI(), ","));
advert.sign(SLPCore.CONFIG.getSPI());
- if (advert.getSize()>CONFIG.getMTU()){
- //crap, too big, have to send a bunch of individual small ones...
- String[] spis = SLPUtils.stringToStringArray(SLPCore.CONFIG.getSPI(), ",");
- for (String spi : spis){
- advert.setSPIs(new String[]{spi});
+ if (advert.getSize() > CONFIG.getMTU()) {
+ // crap, too big, have to send a bunch of
+ // individual small ones...
+ final String[] spis = SLPUtils
+ .stringToStringArray(SLPCore.CONFIG
+ .getSPI(), ",");
+ for (final String spi : spis) {
+ advert.setSPIs(new String[] { spi });
advert.sign(spi);
- InetSocketAddress addr = new InetSocketAddress(SLP_MCAST_ADDRESS,SLP_PORT);
- SLPCore.sendUnicastMessage(advert, addr, false);
+ final InetSocketAddress addr = new InetSocketAddress(
+ SLP_MCAST_ADDRESS, SLP_PORT);
+ SLPCore.sendUnicastMessage(advert, addr,
+ false);
}
- lastUnsolicitedAdvertisement = System.currentTimeMillis();
+ lastUnsolicitedAdvertisement = System
+ .currentTimeMillis();
continue;
}
}
- InetSocketAddress addr = new InetSocketAddress(SLP_MCAST_ADDRESS,SLP_PORT);
+ final InetSocketAddress addr = new InetSocketAddress(
+ SLP_MCAST_ADDRESS, SLP_PORT);
SLPCore.sendUnicastMessage(advert, addr, false);
- lastUnsolicitedAdvertisement = System.currentTimeMillis();
+ lastUnsolicitedAdvertisement = System
+ .currentTimeMillis();
} else {
Thread.sleep(remaining);
}
- }catch (Exception e){
+ } catch (final Exception e) {
continue;
}
}
}
}
-
-
-
+
}
Modified: directory/sandbox/slp/src/main/java/org/apache/directory/slp/impl/da/DirectoryAgentImpl.java
URL: http://svn.apache.org/viewvc/directory/sandbox/slp/src/main/java/org/apache/directory/slp/impl/da/DirectoryAgentImpl.java?rev=782980&r1=782979&r2=782980&view=diff
==============================================================================
--- directory/sandbox/slp/src/main/java/org/apache/directory/slp/impl/da/DirectoryAgentImpl.java (original)
+++ directory/sandbox/slp/src/main/java/org/apache/directory/slp/impl/da/DirectoryAgentImpl.java Tue Jun 9 12:50:45 2009
@@ -39,195 +39,230 @@
import org.apache.directory.slp.messages.ServiceDeregistrationMessage;
import org.apache.directory.slp.messages.ServiceRegistrationMessage;
-
/**
*
* @author Lorenz Breu
*/
-public class DirectoryAgentImpl implements DirectoryAgent{
+public class DirectoryAgentImpl implements DirectoryAgent {
+
+ public DirectoryAgentImpl() {
- public DirectoryAgentImpl(){
-
}
-
-
- /* (non-Javadoc)
+
+ /*
+ * (non-Javadoc)
+ *
* @see ch.ethz.iks.slp.DirectoryAgent#listServices()
*/
public List<Service> listServices() {
return SLPCore.daDaemon.listServices();
}
-
- /* (non-Javadoc)
+ /*
+ * (non-Javadoc)
+ *
* @see ch.ethz.iks.slp.DirectoryAgent#shutdown()
*/
public void shutdown() {
SLPCore.getDirectoryAgentDaemon().shutdown();
-
- }
+ }
- /* (non-Javadoc)
- * @see ch.ethz.iks.slp.DirectoryAgent#registerService(ch.ethz.iks.slp.messages.ServiceRegistrationMessage)
+ /*
+ * (non-Javadoc)
+ *
+ * @see
+ * ch.ethz.iks.slp.DirectoryAgent#registerService(ch.ethz.iks.slp.messages
+ * .ServiceRegistrationMessage)
*/
- public void registerService(ServiceRegistrationMessage reg) throws ServiceLocationException{
+ public void registerService(final ServiceRegistrationMessage reg)
+ throws ServiceLocationException {
SLPCore.getDirectoryAgentDaemon().registerService(reg);
}
-
-
- /* (non-Javadoc)
- * @see ch.ethz.iks.slp.DirectoryAgent#registerService(ch.ethz.iks.slp.Service)
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see
+ * ch.ethz.iks.slp.DirectoryAgent#registerService(ch.ethz.iks.slp.Service)
*/
- public void registerService(Service service) throws ServiceLocationException {
+ public void registerService(final Service service)
+ throws ServiceLocationException {
SLPCore.getDirectoryAgentDaemon().registerService(service);
}
- /* (non-Javadoc)
- * @see ch.ethz.iks.slp.DirectoryAgent#deregisterService(ch.ethz.iks.slp.messages.ServiceDeregistrationMessage)
+ /*
+ * (non-Javadoc)
+ *
+ * @see
+ * ch.ethz.iks.slp.DirectoryAgent#deregisterService(ch.ethz.iks.slp.messages
+ * .ServiceDeregistrationMessage)
*/
- public void deregisterService(ServiceDeregistrationMessage dereg) throws ServiceLocationException{
+ public void deregisterService(final ServiceDeregistrationMessage dereg)
+ throws ServiceLocationException {
SLPCore.getDirectoryAgentDaemon().deregisterService(dereg);
-
+
}
-
- /* (non-Javadoc)
- * @see ch.ethz.iks.slp.DirectoryAgent#deregisterService(ch.ethz.iks.slp.Service)
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see
+ * ch.ethz.iks.slp.DirectoryAgent#deregisterService(ch.ethz.iks.slp.Service)
*/
- public void deregisterService(Service service) throws ServiceLocationException{
+ public void deregisterService(final Service service)
+ throws ServiceLocationException {
SLPCore.getDirectoryAgentDaemon().deregisterService(service);
}
-
-
-// #comment
-// ;comment
-// service-url,language-tag,lifetime,[service-type]<newline>
-// "scopes="[scope-list]<newline>
-// [attrid]"="val1<newline>
-// [attrid]"="val1,val2,val3<newline>
-// <newline>
-
-
- /* (non-Javadoc)
- * @see ch.ethz.iks.slp.DirectoryAgent#registerServicesFromFile(java.io.File)
+
+ // #comment
+ // ;comment
+ // service-url,language-tag,lifetime,[service-type]<newline>
+ // "scopes="[scope-list]<newline>
+ // [attrid]"="val1<newline>
+ // [attrid]"="val1,val2,val3<newline>
+ // <newline>
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see
+ * ch.ethz.iks.slp.DirectoryAgent#registerServicesFromFile(java.io.File)
*/
- public void registerServicesFromFile(File file){
+ public void registerServicesFromFile(final File file) {
try {
- BufferedReader input = new BufferedReader(new FileReader(file));
+ final BufferedReader input = new BufferedReader(
+ new FileReader(file));
String line = null;
boolean parsingService = false;
Service service = null;
List<String> attributes = new ArrayList<String>();
- while ((line = input.readLine()) != null){
- if (line.startsWith("#") || line.startsWith(";")){
+ while ((line = input.readLine()) != null) {
+ if (line.startsWith("#") || line.startsWith(";")) {
// just a comment
continue;
}
- if (!parsingService){
- String[] fields = line.split(",");
- String url = fields[0];
- //TODO: implement correct localization
- //String lang = fields[1];
- int lifetime = Integer.parseInt(fields[2]);
- if (fields.length==4){
- String type = fields[3];
+ if (!parsingService) {
+ final String[] fields = line.split(",");
+ final String url = fields[0];
+ // TODO: implement correct localization
+ // String lang = fields[1];
+ final int lifetime = Integer.parseInt(fields[2]);
+ if (fields.length == 4) {
+ final String type = fields[3];
service = new Service(new ServiceURL(type, lifetime));
} else {
- service = new Service(new ServiceURL(url,lifetime));
+ service = new Service(new ServiceURL(url, lifetime));
}
parsingService = true;
continue;
}
- if (line.equals("")){
- service.setAttributes(SLPUtils.stringArrayToDict(SLPUtils.listToStringArray(attributes)));
+ if (line.equals("")) {
+ service.setAttributes(SLPUtils.stringArrayToDict(SLPUtils
+ .listToStringArray(attributes)));
SLPCore.getDirectoryAgentDaemon().registerService(service);
service = null;
attributes = new ArrayList<String>();
parsingService = false;
continue;
}
- if (line.startsWith("scopes=") && parsingService){
- String[] scopes = line.substring(7).split(",");
+ if (line.startsWith("scopes=") && parsingService) {
+ final String[] scopes = line.substring(7).split(",");
service.setScopes(SLPUtils.arrayToList(scopes));
continue;
}
- if (parsingService){
- attributes.add("("+line+")");
+ if (parsingService) {
+ attributes.add("(" + line + ")");
}
-
+
}
-
-
- } catch (Exception e){
+
+ } catch (final Exception e) {
SLPCore.getPlatform().logError("Unable to parse registration file");
- System.out.println("Unable to parse registration file "+file);
+ System.out.println("Unable to parse registration file " + file);
}
}
-
- /* (non-Javadoc)
- * @see ch.ethz.iks.slp.DirectoryAgent#exportServicesToFile(java.lang.String, boolean)
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see
+ * ch.ethz.iks.slp.DirectoryAgent#exportServicesToFile(java.lang.String,
+ * boolean)
*/
- public void exportServicesToFile(String path, boolean append){
+ public void exportServicesToFile(final String path, final boolean append) {
File file = null;
BufferedWriter out = null;
try {
file = new File(path);
- out = new BufferedWriter(new FileWriter(file,append));
- } catch (Exception e){
- SLPCore.getPlatform().logError("Unable to open registration file for writing");
- System.out.println("Unable to open registration file "+path+" for writing");
+ out = new BufferedWriter(new FileWriter(file, append));
+ } catch (final Exception e) {
+ SLPCore.getPlatform().logError(
+ "Unable to open registration file for writing");
+ System.out.println("Unable to open registration file " + path
+ + " for writing");
}
-
- try{
- List<Service> allServices = listServices();
- for (Service service : allServices){
- //TODO: iplement correct localization
- out.write(service.getURL().toString()+","+SLPCore.DEFAULT_LOCALE+","+service.getURL().getLifetime());
+
+ try {
+ final List<Service> allServices = listServices();
+ for (final Service service : allServices) {
+ // TODO: iplement correct localization
+ out.write(service.getURL().toString() + ","
+ + SLPCore.DEFAULT_LOCALE + ","
+ + service.getURL().getLifetime());
out.newLine();
- out.write("scopes="+SLPUtils.listToString(service.getScopes(),","));
+ out.write("scopes="
+ + SLPUtils.listToString(service.getScopes(), ","));
out.newLine();
- Enumeration<String> keys = service.getAttributesAsStringDict().keys();
- while (keys.hasMoreElements()){
- String key = keys.nextElement();
- out.write(key+"="+service.getAttributesAsStringDict().get(key));
+ final Enumeration<String> keys = service
+ .getAttributesAsStringDict().keys();
+ while (keys.hasMoreElements()) {
+ final String key = keys.nextElement();
+ out.write(key + "="
+ + service.getAttributesAsStringDict().get(key));
out.newLine();
}
out.newLine();
out.close();
-
+
}
- } catch (Exception e){
+ } catch (final Exception e) {
SLPCore.getPlatform().logError("Unable to export to file");
- System.out.println("Unable to export to file "+path);
+ System.out.println("Unable to export to file " + path);
}
-
- }
+ }
- /* (non-Javadoc)
- * @see ch.ethz.iks.slp.DirectoryAgent#setServiceStore(ch.ethz.iks.slp.ServiceStore, boolean)
+ /*
+ * (non-Javadoc)
+ *
+ * @see
+ * ch.ethz.iks.slp.DirectoryAgent#setServiceStore(ch.ethz.iks.slp.ServiceStore
+ * , boolean)
*/
- public void setServiceStore(ServiceStore store, boolean flush) throws ServiceLocationException {
+ public void setServiceStore(final ServiceStore store, final boolean flush)
+ throws ServiceLocationException {
List<Service> services = new ArrayList<Service>();
- if (!flush){
+ if (!flush) {
services = SLPCore.daDaemon.listServices();
}
SLPCore.daDaemon.setStore(store);
- if (!flush){
- for (Service s:services){
+ if (!flush) {
+ for (final Service s : services) {
registerService(s);
}
}
-
+
}
-
-
- /* (non-Javadoc)
+
+ /*
+ * (non-Javadoc)
+ *
* @see ch.ethz.iks.slp.DirectoryAgent#getAttributeTypes()
*/
- public Map<String, Integer> getAttributeTypes() throws ServiceLocationException{
+ public Map<String, Integer> getAttributeTypes()
+ throws ServiceLocationException {
return SLPCore.getDirectoryAgentDaemon().getAttributeTypes();
}
-
-
+
}
Modified: directory/sandbox/slp/src/main/java/org/apache/directory/slp/impl/da/SimpleServiceStore.java
URL: http://svn.apache.org/viewvc/directory/sandbox/slp/src/main/java/org/apache/directory/slp/impl/da/SimpleServiceStore.java?rev=782980&r1=782979&r2=782980&view=diff
==============================================================================
--- directory/sandbox/slp/src/main/java/org/apache/directory/slp/impl/da/SimpleServiceStore.java (original)
+++ directory/sandbox/slp/src/main/java/org/apache/directory/slp/impl/da/SimpleServiceStore.java Tue Jun 9 12:50:45 2009
@@ -35,289 +35,345 @@
import org.apache.directory.slp.impl.SLPUtils;
import org.apache.directory.slp.impl.filter.Filter;
-
/**
- * A simple HashMap based ServiceStore for the DirectoryAgentDaemon to store registrations in.
+ * A simple HashMap based ServiceStore for the DirectoryAgentDaemon to store
+ * registrations in.
*
* @author Lorenz Breu
*/
-public class SimpleServiceStore implements ServiceStore{
-
-
+public class SimpleServiceStore implements ServiceStore {
+
// The services registered
- private Map<String,Service> registeredServices = new HashMap<String,Service>();
-
+ private Map<String, Service> registeredServices = new HashMap<String, Service>();
+
// the type registry
- private Map<String,Integer> attributeTypes = new HashMap<String, Integer>();
-
-
-
- public SimpleServiceStore(){
-
+ private Map<String, Integer> attributeTypes = new HashMap<String, Integer>();
+
+ public SimpleServiceStore() {
+
}
- /* (non-Javadoc)
+ /*
+ * (non-Javadoc)
+ *
* @see ch.ethz.iks.slp.ServiceStore#deleteService(java.lang.String)
*/
- public synchronized void deleteService(String serviceurl) throws ServiceLocationException {
+ public synchronized void deleteService(final String serviceurl)
+ throws ServiceLocationException {
try {
registeredServices.remove(serviceurl);
- } catch (Exception e){
- throw new ServiceLocationException(ServiceLocationException.INTERNAL_SYSTEM_ERROR,"Service not registered before.");
+ } catch (final Exception e) {
+ throw new ServiceLocationException(
+ ServiceLocationException.INTERNAL_SYSTEM_ERROR,
+ "Service not registered before.");
}
-
+
}
-
- /* (non-Javadoc)
- * @see ch.ethz.iks.slp.ServiceStore#getAttributes(java.lang.String, java.lang.String[], java.lang.String[])
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see ch.ethz.iks.slp.ServiceStore#getAttributes(java.lang.String,
+ * java.lang.String[], java.lang.String[])
*/
- public String[] getAttributes(String service, String[] tagList, String[] scopeList){
+ public String[] getAttributes(final String service, final String[] tagList,
+ final String[] scopeList) {
try {
- List<Service> allServiceMatches = getServices(service, "",scopeList);
-
-
- List<String> attResult = new ArrayList<String>();
-
- for (Service matchedService: allServiceMatches) {
- List<String> completeListwithDuplicates = SLPUtils.findMatches(SLPUtils.arrayToList(tagList), matchedService.getAttributesAsStringDict());
-
- for (String s: completeListwithDuplicates){
- if (!attResult.contains(s)){
+ final List<Service> allServiceMatches = getServices(service, "",
+ scopeList);
+
+ final List<String> attResult = new ArrayList<String>();
+
+ for (final Service matchedService : allServiceMatches) {
+ final List<String> completeListwithDuplicates = SLPUtils
+ .findMatches(SLPUtils.arrayToList(tagList),
+ matchedService.getAttributesAsStringDict());
+
+ for (final String s : completeListwithDuplicates) {
+ if (!attResult.contains(s)) {
attResult.add(s);
}
}
-
+
}
-
-
-
- return attResult.toArray(new String[]{});
- } catch (ServiceLocationException sle){
+
+ return attResult.toArray(new String[] {});
+ } catch (final ServiceLocationException sle) {
SLPCore.getPlatform().logError("error finding attributes", sle);
- return new String[]{""};
+ return new String[] { "" };
}
}
-
- /* (non-Javadoc)
- * @see ch.ethz.iks.slp.ServiceStore#getServiceTypes(java.lang.String[], java.lang.String)
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see ch.ethz.iks.slp.ServiceStore#getServiceTypes(java.lang.String[],
+ * java.lang.String)
*/
- public ServiceType[] getServiceTypes(String[] scopes, String namingAuthority){
+ public ServiceType[] getServiceTypes(final String[] scopes,
+ final String namingAuthority) {
try {
- List<Service> allServiceMatches = listServices();
- List<Service> scopeMatchedServices = new ArrayList<Service>();
- for (Service serv: allServiceMatches){
- for (String scope : scopes){
- if (serv.getScopes().contains(scope.toLowerCase().trim())){
+ final List<Service> allServiceMatches = listServices();
+ final List<Service> scopeMatchedServices = new ArrayList<Service>();
+ for (final Service serv : allServiceMatches) {
+ for (final String scope : scopes) {
+ if (serv.getScopes().contains(scope.toLowerCase().trim())) {
scopeMatchedServices.add(serv);
break;
}
}
-
+
}
-
- ArrayList<ServiceType> result = new ArrayList<ServiceType>();
-
- for (Service service : scopeMatchedServices) {
- ServiceType type = service.getURL().getServiceType();
- if (namingAuthority.equals("*")
- || namingAuthority.equals("")
- || type.getNamingAuthority().equals(
- namingAuthority)) {
+
+ final ArrayList<ServiceType> result = new ArrayList<ServiceType>();
+
+ for (final Service service : scopeMatchedServices) {
+ final ServiceType type = service.getURL().getServiceType();
+ if (namingAuthority.equals("*") || namingAuthority.equals("")
+ || type.getNamingAuthority().equals(namingAuthority)) {
if (!result.contains(type)) {
result.add(type);
}
}
}
-
- return result.toArray(new ServiceType[]{});
- } catch (ServiceLocationException sle){
- SLPCore.getPlatform().logError("Error while retrieving service types", sle);
- return new ServiceType[]{};
+
+ return result.toArray(new ServiceType[] {});
+ } catch (final ServiceLocationException sle) {
+ SLPCore.getPlatform().logError(
+ "Error while retrieving service types", sle);
+ return new ServiceType[] {};
}
}
- /* (non-Javadoc)
- * @see ch.ethz.iks.slp.ServiceStore#getServices(java.lang.String, java.lang.String)
+ /*
+ * (non-Javadoc)
+ *
+ * @see ch.ethz.iks.slp.ServiceStore#getServices(java.lang.String,
+ * java.lang.String)
*/
- public List<Service> getServices(String serviceType,String attributeFilter) throws ServiceLocationException {
+ public List<Service> getServices(final String serviceType,
+ final String attributeFilter) throws ServiceLocationException {
try {
- return getServices(serviceType, attributeFilter, SLPUtils.stringToStringArray(SLPCore.CONFIG.getScopes(),","));
- } catch (Exception e){
- throw new ServiceLocationException(ServiceLocationException.INTERNAL_SYSTEM_ERROR,"Error while retrieveing Services");
- }
-
- }
-
-
- /* (non-Javadoc)
- * @see ch.ethz.iks.slp.ServiceStore#getServices(java.lang.String, java.lang.String, java.lang.String[])
+ return getServices(serviceType, attributeFilter, SLPUtils
+ .stringToStringArray(SLPCore.CONFIG.getScopes(), ","));
+ } catch (final Exception e) {
+ throw new ServiceLocationException(
+ ServiceLocationException.INTERNAL_SYSTEM_ERROR,
+ "Error while retrieveing Services");
+ }
+
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see ch.ethz.iks.slp.ServiceStore#getServices(java.lang.String,
+ * java.lang.String, java.lang.String[])
*/
- public List<Service> getServices(String serviceType,String attributeFilter, String[] scopes) throws ServiceLocationException {
+ public List<Service> getServices(final String serviceType,
+ final String attributeFilter, final String[] scopes)
+ throws ServiceLocationException {
try {
- List<String> scopeList = new ArrayList<String>();
- for (String s: scopes){
+ final List<String> scopeList = new ArrayList<String>();
+ for (final String s : scopes) {
scopeList.add(s.toLowerCase().trim());
}
-
+
Object st;
- try{
- st = new ServiceURL(serviceType,0);
- } catch (Exception e){
+ try {
+ st = new ServiceURL(serviceType, 0);
+ } catch (final Exception e) {
st = new ServiceType(serviceType);
}
- List<Service> results = new ArrayList<Service>();
- synchronized(registeredServices){
- for (String key : registeredServices.keySet()) {
- Service service = registeredServices.get(key);
-
- if (service.getURL().matches(st) && scopeMatch(service.getScopes(),scopeList)) {
- if (attributeFilter == null || attributeFilter.equals("")) {
-
+ final List<Service> results = new ArrayList<Service>();
+ synchronized (registeredServices) {
+ for (final String key : registeredServices.keySet()) {
+ final Service service = registeredServices.get(key);
+
+ if (service.getURL().matches(st)
+ && scopeMatch(service.getScopes(), scopeList)) {
+ if (attributeFilter == null
+ || attributeFilter.equals("")) {
+
results.add(service);
continue;
}
- Filter filter = SLPCore.getPlatform().createFilter(attributeFilter);
- if (filter.match(service.getAttributes()) && scopeMatch(service.getScopes(),scopeList)) {
+ final Filter filter = SLPCore.getPlatform()
+ .createFilter(attributeFilter);
+ if (filter.match(service.getAttributes())
+ && scopeMatch(service.getScopes(), scopeList)) {
results.add(service);
}
}
}
}
-
-
+
return results;
-
- } catch (Exception e){
- throw new ServiceLocationException(ServiceLocationException.INTERNAL_SYSTEM_ERROR,"Error while retrieveing Services");
+
+ } catch (final Exception e) {
+ throw new ServiceLocationException(
+ ServiceLocationException.INTERNAL_SYSTEM_ERROR,
+ "Error while retrieveing Services");
}
-
+
}
-
- /* (non-Javadoc)
+
+ /*
+ * (non-Javadoc)
+ *
* @see ch.ethz.iks.slp.ServiceStore#getService(java.lang.String)
*/
- public synchronized Service getService(String serviceUrl) throws ServiceLocationException{
+ public synchronized Service getService(final String serviceUrl)
+ throws ServiceLocationException {
try {
return registeredServices.get(serviceUrl);
- } catch (Exception e){
- throw new ServiceLocationException(ServiceLocationException.INTERNAL_SYSTEM_ERROR,"Failed to find specified service");
+ } catch (final Exception e) {
+ throw new ServiceLocationException(
+ ServiceLocationException.INTERNAL_SYSTEM_ERROR,
+ "Failed to find specified service");
}
}
- /* (non-Javadoc)
+ /*
+ * (non-Javadoc)
+ *
* @see ch.ethz.iks.slp.ServiceStore#storeService(ch.ethz.iks.slp.Service)
*/
- public synchronized void storeService(Service service) throws ServiceLocationException {
- if (registeredServices.get(service.getURL().toString())!=null){
- throw new ServiceLocationException(ServiceLocationException.INTERNAL_SYSTEM_ERROR,"Service already exists");
- }
- String abstracttype = service.getURL().getServiceType().getAbstractTypeName();
- if (abstracttype.equals("")){
- abstracttype=service.getURL().toString();
- abstracttype = abstracttype.substring(0,abstracttype.indexOf("://"));
- ServiceType st = new ServiceType(abstracttype);
- abstracttype=st.getAbstractTypeName();
+ public synchronized void storeService(final Service service)
+ throws ServiceLocationException {
+ if (registeredServices.get(service.getURL().toString()) != null) {
+ throw new ServiceLocationException(
+ ServiceLocationException.INTERNAL_SYSTEM_ERROR,
+ "Service already exists");
+ }
+ String abstracttype = service.getURL().getServiceType()
+ .getAbstractTypeName();
+ if (abstracttype.equals("")) {
+ abstracttype = service.getURL().toString();
+ abstracttype = abstracttype.substring(0, abstracttype
+ .indexOf("://"));
+ final ServiceType st = new ServiceType(abstracttype);
+ abstracttype = st.getAbstractTypeName();
}
- if (abstracttype.startsWith("service:")){
+ if (abstracttype.startsWith("service:")) {
abstracttype = abstracttype.substring("service:".length());
}
try {
- if (service.getAttributes()!=null){
- Enumeration<String> atts = service.getAttributes().keys();
- while (atts.hasMoreElements()){
- String k = atts.nextElement();
+ if (service.getAttributes() != null) {
+ final Enumeration<String> atts = service.getAttributes().keys();
+ while (atts.hasMoreElements()) {
+ final String k = atts.nextElement();
int type;
- if ((service.getAttributes().get(k) instanceof Boolean) || service.getAttributes().get(k) instanceof Boolean[]){
+ if (service.getAttributes().get(k) instanceof Boolean
+ || service.getAttributes().get(k) instanceof Boolean[]) {
type = BOOLEAN_AT;
- } else if ((service.getAttributes().get(k) instanceof Number) || service.getAttributes().get(k) instanceof Number[]){
+ } else if (service.getAttributes().get(k) instanceof Number
+ || service.getAttributes().get(k) instanceof Number[]) {
type = INTEGER_AT;
- } else if ((service.getAttributes().get(k) instanceof OpaqueValue) || service.getAttributes().get(k) instanceof OpaqueValue[]){
+ } else if (service.getAttributes().get(k) instanceof OpaqueValue
+ || service.getAttributes().get(k) instanceof OpaqueValue[]) {
type = OPAQUE_AT;
} else {
- type=STRING_AT;
+ type = STRING_AT;
}
- Integer at = attributeTypes.get(abstracttype+"."+k);
- if (at!=null && at!=type){
- throw new ServiceLocationException(ServiceLocationException.INVALID_REGISTRATION,"inconsistent attribute type");
+ final Integer at = attributeTypes.get(abstracttype + "."
+ + k);
+ if (at != null && at != type) {
+ throw new ServiceLocationException(
+ ServiceLocationException.INVALID_REGISTRATION,
+ "inconsistent attribute type");
}
- attributeTypes.put(abstracttype+"."+k, type);
-
+ attributeTypes.put(abstracttype + "." + k, type);
+
}
}
- registeredServices.put(service.getURL().toString(),service);
-
-
- } catch (Exception e){
- if (e instanceof ServiceLocationException){
+ registeredServices.put(service.getURL().toString(), service);
+
+ } catch (final Exception e) {
+ if (e instanceof ServiceLocationException) {
throw (ServiceLocationException) e;
}
- throw new ServiceLocationException(ServiceLocationException.INTERNAL_SYSTEM_ERROR,"Problems while registering service");
+ throw new ServiceLocationException(
+ ServiceLocationException.INTERNAL_SYSTEM_ERROR,
+ "Problems while registering service");
}
-
+
}
- /* (non-Javadoc)
+ /*
+ * (non-Javadoc)
+ *
* @see ch.ethz.iks.slp.ServiceStore#containsService(java.lang.String)
*/
- public synchronized boolean containsService(String service) {
+ public synchronized boolean containsService(final String service) {
return registeredServices.containsKey(service);
}
- /* (non-Javadoc)
+ /*
+ * (non-Javadoc)
+ *
* @see ch.ethz.iks.slp.ServiceStore#listServices()
*/
- public synchronized List<Service> listServices() throws ServiceLocationException {
- List<Service> result = new ArrayList<Service>();
- for (Service service : registeredServices.values()){
+ public synchronized List<Service> listServices()
+ throws ServiceLocationException {
+ final List<Service> result = new ArrayList<Service>();
+ for (final Service service : registeredServices.values()) {
result.add(service);
}
return result;
}
-
- /* (non-Javadoc)
+ /*
+ * (non-Javadoc)
+ *
* @see ch.ethz.iks.slp.ServiceStore#clear()
*/
- public synchronized void clear(){
- registeredServices = new HashMap<String,Service>();
+ public synchronized void clear() {
+ registeredServices = new HashMap<String, Service>();
attributeTypes = new HashMap<String, Integer>();
}
-
+
/**
- * Checks if there is a scope in both lists. Returns true if one of the two is null....
+ * Checks if there is a scope in both lists. Returns true if one of the two
+ * is null....
*
* @param scopes1
- * A List of scopes strings
+ * A List of scopes strings
* @param scopes2
- * A List of scopes strings
- * @return
- * True if either list is null or both lists contain at least one identical scope
- * False if both lists contain unique scopes
- */
- private boolean scopeMatch(List<String> scopes1, List<String> scopes2){
- if ((scopes1==null) || (scopes2==null)){
+ * A List of scopes strings
+ * @return True if either list is null or both lists contain at least one
+ * identical scope False if both lists contain unique scopes
+ */
+ private boolean scopeMatch(final List<String> scopes1,
+ final List<String> scopes2) {
+ if (scopes1 == null || scopes2 == null) {
return true;
}
- for (String s: scopes1){
- if (scopes2.contains(s)){
+ for (final String s : scopes1) {
+ if (scopes2.contains(s)) {
return true;
}
}
return false;
}
-
- /* (non-Javadoc)
+
+ /*
+ * (non-Javadoc)
+ *
* @see ch.ethz.iks.slp.ServiceStore#shutdown()
*/
- public void shutdown(){
+ public void shutdown() {
// nothing to do
}
-
- /* (non-Javadoc)
+
+ /*
+ * (non-Javadoc)
+ *
* @see ch.ethz.iks.slp.ServiceStore#getAttributeTypes()
*/
- public Map<String, Integer> getAttributeTypes() throws ServiceLocationException{
+ public Map<String, Integer> getAttributeTypes()
+ throws ServiceLocationException {
return new HashMap<String, Integer>(attributeTypes);
}
}