Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorslewis2008-11-03 14:16:07 -0500
committerslewis2008-11-03 14:16:07 -0500
commit6d0193bd045509af43a087c3d9c87a6d8d4cf0b8 (patch)
treec40f578e7b9c790c17feb33c48dc702e208b0fa5
parent52def99ed72c1ee8e2e7130a1beb90d4927b1d63 (diff)
downloadorg.eclipse.ecf-6d0193bd045509af43a087c3d9c87a6d8d4cf0b8.tar.gz
org.eclipse.ecf-6d0193bd045509af43a087c3d9c87a6d8d4cf0b8.tar.xz
org.eclipse.ecf-6d0193bd045509af43a087c3d9c87a6d8d4cf0b8.zip
added AbstractGenericServer class
-rw-r--r--server-side/bundles/org.eclipse.ecf.server.generic/src/org/eclipse/ecf/server/generic/AbstractGenericServer.java117
1 files changed, 117 insertions, 0 deletions
diff --git a/server-side/bundles/org.eclipse.ecf.server.generic/src/org/eclipse/ecf/server/generic/AbstractGenericServer.java b/server-side/bundles/org.eclipse.ecf.server.generic/src/org/eclipse/ecf/server/generic/AbstractGenericServer.java
new file mode 100644
index 000000000..c66f3ecb7
--- /dev/null
+++ b/server-side/bundles/org.eclipse.ecf.server.generic/src/org/eclipse/ecf/server/generic/AbstractGenericServer.java
@@ -0,0 +1,117 @@
+package org.eclipse.ecf.server.generic;
+
+import java.io.IOException;
+import java.security.PermissionCollection;
+import java.util.*;
+import org.eclipse.ecf.core.identity.*;
+import org.eclipse.ecf.core.security.IConnectHandlerPolicy;
+import org.eclipse.ecf.core.sharedobject.ISharedObjectContainerConfig;
+import org.eclipse.ecf.provider.generic.*;
+
+public abstract class AbstractGenericServer {
+
+ protected class Server extends TCPServerSOContainer {
+
+ public Server(ISharedObjectContainerConfig config, TCPServerSOContainerGroup listener, String path, int keepAlive) {
+ super(config, listener, path, keepAlive);
+ }
+
+ }
+
+ protected TCPServerSOContainerGroup serverGroup;
+
+ public List getServers() {
+ List result = new ArrayList();
+ for (Iterator i = serverGroup.elements(); i.hasNext();) {
+ result.add(i.next());
+ }
+ return result;
+ }
+
+ public Server getServer() {
+ return (Server) getServers().get(0);
+ }
+
+ public AbstractGenericServer(String host, int port) {
+ this.serverGroup = new TCPServerSOContainerGroup(host, port);
+ }
+
+ protected void putOnTheAir() throws IOException {
+ if (!serverGroup.isOnTheAir())
+ serverGroup.putOnTheAir();
+ }
+
+ protected void takeOffTheAir() {
+ if (serverGroup.isOnTheAir())
+ serverGroup.takeOffTheAir();
+ }
+
+ public synchronized void start(String path, int keepAlive) throws Exception {
+ createAndInitializeServer(path, keepAlive);
+ putOnTheAir();
+ }
+
+ public synchronized void stop() {
+ if (serverGroup != null) {
+ serverGroup.takeOffTheAir();
+ }
+ List servers = getServers();
+ for (Iterator i = servers.iterator(); i.hasNext();) {
+ Server s = (Server) i.next();
+ s.ejectAllGroupMembers("Shutting down immediately"); //$NON-NLS-1$
+ s.dispose();
+ }
+ }
+
+ protected void createAndInitializeServer(String path) throws IDCreateException {
+ createAndInitializeServer(path, TCPServerSOContainer.DEFAULT_KEEPALIVE);
+ }
+
+ protected void createAndInitializeServer(String path, int keepAlive) throws IDCreateException {
+ if (path == null || path.equals("")) //$NON-NLS-1$
+ throw new NullPointerException("Cannot create ID with null or empty path"); //$NON-NLS-1$
+ Server s = new Server(createServerConfig(path), serverGroup, path, keepAlive);
+ IConnectHandlerPolicy policy = createConnectHandlerPolicy(s, path);
+ if (policy != null)
+ s.setConnectPolicy(policy);
+ System.out.println("starting generic server " + s.getID().getName()); //$NON-NLS-1$
+ }
+
+ protected PermissionCollection checkConnect(Object address, ID fromID, ID targetID, String targetGroup, Object connectData) throws Exception {
+ // Report connect
+ System.out.println("connect addr=" + address + ",from=" + fromID + ",to=" + targetID + ",group=" + targetGroup); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
+ return null;
+ }
+
+ protected IConnectHandlerPolicy createConnectHandlerPolicy(Server s, String path) {
+ return new IConnectHandlerPolicy() {
+ public PermissionCollection checkConnect(Object address, ID fromID, ID targetID, String targetGroup, Object connectData) throws Exception {
+ return AbstractGenericServer.this.checkConnect(address, fromID, targetID, targetGroup, connectData);
+ }
+
+ public void refresh() {
+ // do nothing
+ }
+ };
+ }
+
+ protected ID createServerIDFromPath(String path) throws IDCreateException {
+ if (!path.startsWith("/"))path = "/" + path; //$NON-NLS-1$//$NON-NLS-2$
+ String id = TCPServerSOContainer.DEFAULT_PROTOCOL + "://" //$NON-NLS-1$
+ + getHost() + ":" + getPort() + path; //$NON-NLS-1$
+ return IDFactory.getDefault().createStringID(id);
+ }
+
+ protected SOContainerConfig createServerConfig(String path) throws IDCreateException {
+ return new SOContainerConfig(createServerIDFromPath(path));
+ }
+
+ protected String getHost() {
+ return this.serverGroup.getName();
+ }
+
+ protected int getPort() {
+ return this.serverGroup.getPort();
+ }
+
+}

Back to the top