You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@directory.apache.org by er...@apache.org on 2005/05/25 09:15:33 UTC
svn commit: r178397 - in
/directory/sandbox/trunk/osgi-protocol-providers/trunk/changepw/src/main/java/org/apache/changepw:
Activator.java ChangepwConfig.java ChangepwServer.java
ChangepwServerFactory.java
Author: erodriguez
Date: Wed May 25 00:15:32 2005
New Revision: 178397
URL: http://svn.apache.org/viewcvs?rev=178397&view=rev
Log:
OSGi wrapper for Change Password prototol provider.
Added:
directory/sandbox/trunk/osgi-protocol-providers/trunk/changepw/src/main/java/org/apache/changepw/Activator.java
directory/sandbox/trunk/osgi-protocol-providers/trunk/changepw/src/main/java/org/apache/changepw/ChangepwConfig.java
directory/sandbox/trunk/osgi-protocol-providers/trunk/changepw/src/main/java/org/apache/changepw/ChangepwServer.java
directory/sandbox/trunk/osgi-protocol-providers/trunk/changepw/src/main/java/org/apache/changepw/ChangepwServerFactory.java
Added: directory/sandbox/trunk/osgi-protocol-providers/trunk/changepw/src/main/java/org/apache/changepw/Activator.java
URL: http://svn.apache.org/viewcvs/directory/sandbox/trunk/osgi-protocol-providers/trunk/changepw/src/main/java/org/apache/changepw/Activator.java?rev=178397&view=auto
==============================================================================
--- directory/sandbox/trunk/osgi-protocol-providers/trunk/changepw/src/main/java/org/apache/changepw/Activator.java (added)
+++ directory/sandbox/trunk/osgi-protocol-providers/trunk/changepw/src/main/java/org/apache/changepw/Activator.java Wed May 25 00:15:32 2005
@@ -0,0 +1,135 @@
+/*
+ * Copyright 2005 The Apache Software Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+package org.apache.changepw;
+
+import java.util.Dictionary;
+import java.util.Hashtable;
+
+import org.apache.kerberos.store.PrincipalStore;
+import org.apache.mina.registry.ServiceRegistry;
+import org.osgi.framework.BundleActivator;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.ServiceReference;
+import org.osgi.framework.ServiceRegistration;
+import org.osgi.service.cm.Configuration;
+import org.osgi.service.cm.ConfigurationAdmin;
+import org.osgi.service.cm.ConfigurationException;
+import org.osgi.service.cm.ManagedServiceFactory;
+import org.osgi.util.tracker.ServiceTracker;
+
+/**
+ * This class implements a simple bundle that starts and stops the Apache Change Password service.
+ */
+public class Activator implements BundleActivator
+{
+ public static final String FACTORY_PID = "org.apache.changepw";
+
+ private ServiceRegistry registry;
+ private ServiceTracker tracker;
+
+ private ChangepwServerFactory serverFactory = null;
+ private ServiceRegistration registration = null;
+
+ /**
+ * Implements BundleActivator.start().
+ * Logs that this service is starting and starts this service.
+ * @param context the framework context for the bundle.
+ */
+ public void start(BundleContext context)
+ {
+ System.out.println("Starting Apache Change Password.");
+
+ tracker = new ServiceTracker(context, ServiceRegistry.class.getName(), null);
+ tracker.open();
+
+ registry = (ServiceRegistry) tracker.getService();
+
+ ServiceReference storeReference = context.getServiceReference(PrincipalStore.class
+ .getName());
+ PrincipalStore store = (PrincipalStore) context.getService(storeReference);
+ System.out.println("PrincipalStore retrieval got " + store);
+
+ serverFactory = new ChangepwServerFactory(registry, store);
+
+ Dictionary parameters = new Hashtable();
+ parameters.put(ConfigurationAdmin.SERVICE_FACTORYPID, FACTORY_PID);
+ registration = context.registerService(ManagedServiceFactory.class.getName(),
+ serverFactory, parameters);
+
+ ServiceReference adminRef = null;
+ try
+ {
+ ConfigurationAdmin admin = null;
+ Configuration[] configs = null;
+ try
+ {
+ adminRef = context.getServiceReference(ConfigurationAdmin.class.getName());
+
+ // Potential start order problem!
+ if (adminRef != null)
+ {
+ admin = (ConfigurationAdmin) context.getService(adminRef);
+ String filter = "(&(service.factoryPid=" + FACTORY_PID + ")"
+ + "(|(service.bundleLocation=" + context.getBundle().getLocation()
+ + ")" + "(service.bundleLocation=NULL)"
+ + "(!(service.bundleLocation=*))))";
+ configs = admin.listConfigurations(filter);
+ }
+ }
+ catch (Exception e)
+ {
+ e.printStackTrace();
+ }
+
+ if (admin == null || configs == null || configs.length == 0)
+ {
+ serverFactory.updated(ChangepwServerFactory.DEFAULT_PID, ChangepwConfig
+ .getDefaultConfig());
+ }
+ }
+ catch (ConfigurationException ce)
+ {
+ ce.printStackTrace();
+ }
+ finally
+ {
+ if (adminRef != null)
+ {
+ context.ungetService(adminRef);
+ }
+ }
+ }
+
+ /**
+ * Implements BundleActivator.stop().
+ * Logs that this service has stopped.
+ * @param context the framework context for the bundle.
+ */
+ public void stop(BundleContext context)
+ {
+ System.out.println("Stopping Apache Change Password.");
+
+ registration.unregister();
+ registration = null;
+
+ serverFactory.destroy();
+ serverFactory = null;
+
+ registry = null;
+ }
+}
Added: directory/sandbox/trunk/osgi-protocol-providers/trunk/changepw/src/main/java/org/apache/changepw/ChangepwConfig.java
URL: http://svn.apache.org/viewcvs/directory/sandbox/trunk/osgi-protocol-providers/trunk/changepw/src/main/java/org/apache/changepw/ChangepwConfig.java?rev=178397&view=auto
==============================================================================
--- directory/sandbox/trunk/osgi-protocol-providers/trunk/changepw/src/main/java/org/apache/changepw/ChangepwConfig.java (added)
+++ directory/sandbox/trunk/osgi-protocol-providers/trunk/changepw/src/main/java/org/apache/changepw/ChangepwConfig.java Wed May 25 00:15:32 2005
@@ -0,0 +1,80 @@
+/*
+ * Copyright 2005 The Apache Software Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+package org.apache.changepw;
+
+import java.util.Dictionary;
+import java.util.Hashtable;
+
+public class ChangepwConfig
+{
+ private static String SERVICE_PID = "service.pid";
+
+ static String PID = "org.apache.changepw";
+ static String CHANGEPW_PORT_KEY = "changepw.port";
+ static int CHANGEPW_PORT_DEFAULT = 464;
+ static String name = "Apache Change Password Service";
+
+ private int port;
+
+ public ChangepwConfig(Dictionary configuration)
+ {
+ System.out.println("Got props " + configuration);
+
+ if (configuration == null)
+ {
+ configuration = getDefaultConfig();
+ }
+
+ port = ((Integer) configuration.get(CHANGEPW_PORT_KEY)).intValue();
+
+ System.out.println("Change Password got port " + port);
+ }
+
+ boolean isDifferent(Dictionary config)
+ {
+ if (port == ((Integer) config.get(CHANGEPW_PORT_KEY)).intValue())
+ {
+ return false;
+ }
+
+ return true;
+ }
+
+ String getName()
+ {
+ return name;
+ }
+
+ int getPort()
+ {
+ System.out.println("Config returning " + port);
+
+ return port;
+ }
+
+ public static Dictionary getDefaultConfig()
+ {
+ Dictionary defaults = new Hashtable();
+
+ defaults.put(SERVICE_PID, PID);
+ defaults.put(CHANGEPW_PORT_KEY, Integer.getInteger(CHANGEPW_PORT_KEY, new Integer(
+ CHANGEPW_PORT_DEFAULT)));
+
+ return defaults;
+ }
+}
Added: directory/sandbox/trunk/osgi-protocol-providers/trunk/changepw/src/main/java/org/apache/changepw/ChangepwServer.java
URL: http://svn.apache.org/viewcvs/directory/sandbox/trunk/osgi-protocol-providers/trunk/changepw/src/main/java/org/apache/changepw/ChangepwServer.java?rev=178397&view=auto
==============================================================================
--- directory/sandbox/trunk/osgi-protocol-providers/trunk/changepw/src/main/java/org/apache/changepw/ChangepwServer.java (added)
+++ directory/sandbox/trunk/osgi-protocol-providers/trunk/changepw/src/main/java/org/apache/changepw/ChangepwServer.java Wed May 25 00:15:32 2005
@@ -0,0 +1,95 @@
+/*
+ * Copyright 2005 The Apache Software Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+package org.apache.changepw;
+
+import java.io.IOException;
+import java.util.Dictionary;
+
+import org.apache.changepw.protocol.ChangePasswordProtocolProvider;
+import org.apache.kerberos.service.KdcConfiguration;
+import org.apache.kerberos.store.PrincipalStore;
+import org.apache.mina.common.TransportType;
+import org.apache.mina.protocol.ProtocolProvider;
+import org.apache.mina.registry.Service;
+import org.apache.mina.registry.ServiceRegistry;
+
+public class ChangepwServer
+{
+ private ServiceRegistry registry;
+ private ProtocolProvider provider;
+ private Service tcpService;
+ private Service udpService;
+
+ private ChangepwConfig config;
+ private PrincipalStore store;
+
+ private String name;
+ private int port = -1;
+
+ public ChangepwServer(ChangepwConfig config, ServiceRegistry registry, PrincipalStore store)
+ {
+ this.config = config;
+ this.registry = registry;
+ this.store = store;
+
+ init();
+ }
+
+ protected void init()
+ {
+ port = config.getPort();
+ name = config.getName();
+
+ try
+ {
+ System.out.println(name + " starting on " + port);
+
+ provider = new ChangePasswordProtocolProvider(new KdcConfiguration(), store);
+
+ udpService = new Service(name, TransportType.DATAGRAM, port);
+ tcpService = new Service(name, TransportType.SOCKET, port);
+
+ registry.bind(udpService, provider);
+ registry.bind(tcpService, provider);
+
+ System.out.println(name + " listening on port " + port);
+ }
+ catch (IOException ioe)
+ {
+ ioe.printStackTrace();
+ }
+ }
+
+ public boolean isDifferent(Dictionary newConfig)
+ {
+ return config.isDifferent(newConfig);
+ }
+
+ public void destroy()
+ {
+ registry.unbind(udpService);
+ registry.unbind(tcpService);
+
+ registry = null;
+ provider = null;
+ udpService = null;
+ tcpService = null;
+
+ System.out.println(name + " has stopped listening on " + port);
+ }
+}
Added: directory/sandbox/trunk/osgi-protocol-providers/trunk/changepw/src/main/java/org/apache/changepw/ChangepwServerFactory.java
URL: http://svn.apache.org/viewcvs/directory/sandbox/trunk/osgi-protocol-providers/trunk/changepw/src/main/java/org/apache/changepw/ChangepwServerFactory.java?rev=178397&view=auto
==============================================================================
--- directory/sandbox/trunk/osgi-protocol-providers/trunk/changepw/src/main/java/org/apache/changepw/ChangepwServerFactory.java (added)
+++ directory/sandbox/trunk/osgi-protocol-providers/trunk/changepw/src/main/java/org/apache/changepw/ChangepwServerFactory.java Wed May 25 00:15:32 2005
@@ -0,0 +1,116 @@
+/*
+ * Copyright 2005 The Apache Software Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+package org.apache.changepw;
+
+import java.util.Dictionary;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.Map;
+
+import org.apache.kerberos.store.PrincipalStore;
+import org.apache.mina.registry.ServiceRegistry;
+import org.osgi.service.cm.ConfigurationException;
+import org.osgi.service.cm.ManagedServiceFactory;
+
+public class ChangepwServerFactory implements ManagedServiceFactory
+{
+ final static String DEFAULT_PID = Activator.FACTORY_PID + ".default";
+
+ private final PrincipalStore store;
+ private final ServiceRegistry registry;
+ private final Map servers = new HashMap();
+
+ Object updateLock = new Object();
+
+ ChangepwServerFactory(ServiceRegistry registry, PrincipalStore store)
+ {
+ this.registry = registry;
+ this.store = store;
+ }
+
+ public void destroy()
+ {
+ Iterator it = servers.keySet().iterator();
+ while (it.hasNext())
+ {
+ deleted((String) it.next());
+ }
+ }
+
+ public void updated(String pid, Dictionary config) throws ConfigurationException
+ {
+ int port = ((Integer) config.get(ChangepwConfig.CHANGEPW_PORT_KEY)).intValue();
+
+ if (port < 1 || port > 0xFFFF)
+ {
+ throw new ConfigurationException(ChangepwConfig.CHANGEPW_PORT_KEY, "invalid value="
+ + port);
+ }
+
+ synchronized (updateLock)
+ {
+ if (DEFAULT_PID.equals(pid) && servers.size() > 0)
+ {
+ return;
+ }
+
+ // As soon as we get a "non-default"-config, delete default
+ if (!DEFAULT_PID.equals(pid) && (null != servers.get(DEFAULT_PID)))
+ {
+ deleted(DEFAULT_PID);
+ }
+
+ // for a given pid-config, do we have the service?
+ // if not, create it with the config
+ ChangepwServer server = (ChangepwServer) servers.get(pid);
+ if (server == null)
+ {
+ server = new ChangepwServer(new ChangepwConfig(config), registry, store);
+ servers.put(pid, server);
+ }
+ else
+ {
+ // we have the service, so ...
+ // for a given config, is the config different?
+ if (server.isDifferent(config))
+ {
+ // the config for this service is different, so ...
+ // destroy the listener and recreate it with the new config.
+ deleted(pid);
+ server = new ChangepwServer(new ChangepwConfig(config), registry, store);
+ servers.put(pid, server);
+ }
+ }
+ }
+ }
+
+ public void deleted(String pid)
+ {
+ ChangepwServer server = (ChangepwServer) servers.remove(pid);
+
+ if (server != null)
+ {
+ server.destroy();
+ }
+ }
+
+ public String getName()
+ {
+ return "Apache Change Password Service Factory";
+ }
+}