KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > fr > dyade > aaa > jndi2 > distributed > DistributedJndiServer


1 /*
2  * JORAM: Java(TM) Open Reliable Asynchronous Messaging
3  * Copyright (C) 2001 - 2003 ScalAgent Distributed Technologies
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
18  * USA.
19  *
20  * Initial developer(s): David Feliot
21  */

22 package fr.dyade.aaa.jndi2.distributed;
23
24 import java.io.*;
25 import java.util.*;
26 import java.net.*;
27 import javax.naming.*;
28
29 import fr.dyade.aaa.jndi2.msg.*;
30 import fr.dyade.aaa.jndi2.impl.*;
31 import fr.dyade.aaa.jndi2.server.*;
32 import fr.dyade.aaa.jndi2.server.Trace;
33 import fr.dyade.aaa.util.*;
34 import fr.dyade.aaa.agent.*;
35 //import fr.dyade.aaa.agent.conf.*;
36

37 import org.objectweb.util.monolog.api.BasicLevel;
38 import org.objectweb.util.monolog.api.Logger;
39
40 /**
41  * Class of a JNDI server that belongs to a distributed JNDI
42  * configuration. All the servers know each other. A naming
43  * context is owned by a unique server and is replicated in
44  * all the servers known by the owner server.
45  */

46 public class DistributedJndiServer {
47
48   private static TcpServer tcpServer;
49
50   public static void init(String JavaDoc args, boolean firstTime) throws Exception JavaDoc {
51     if (Trace.logger.isLoggable(BasicLevel.DEBUG))
52       Trace.logger.log(BasicLevel.DEBUG, "DistributedJndiServer.init(" +
53                        args + ',' + firstTime + ')');
54     StringTokenizer st = new StringTokenizer(args);
55     String JavaDoc portS = st.nextToken();
56     int port = Integer.parseInt(portS);
57
58     Vector list = new Vector();
59     while (st.hasMoreTokens()) {
60       String JavaDoc idS = st.nextToken();
61       Short JavaDoc id = Short.valueOf(idS);
62       list.addElement(id);
63     }
64
65     short[] serverIds = new short[list.size()];
66     for (int i = 0; i < list.size(); i++) {
67       serverIds[i] =
68         ((Short JavaDoc)list.elementAt(i)).shortValue();
69     }
70
71     // Create the socket here in order to throw an exception
72
// if the socket can't be created (even if firstTime is false).
73
ServerSocket serverSocket = new ServerSocket(port);
74
75     int poolSize = Integer.getInteger(
76       JndiServer.POOL_SIZE_PROP,
77       JndiServer.DEFAULT_POOL_SIZE).intValue();
78
79     int timeout = Integer.getInteger(
80       JndiServer.SO_TIMEOUT_PROP,
81       JndiServer.DEFAULT_SO_TIMEOUT).intValue();
82
83     tcpServer = new TcpServer(
84       serverSocket,
85       poolSize,
86       timeout,
87       getDefault());
88
89     if (firstTime) {
90       ReplicationManager manager =
91         new ReplicationManager(serverIds);
92       AgentEntryPoint agentEP = new AgentEntryPoint();
93       agentEP.setRequestManager(manager);
94       TcpEntryPoint tcpEP = new TcpEntryPoint();
95       tcpEP.setRequestManager(manager);
96       ReplicationEntryPoint replicationEP = new ReplicationEntryPoint();
97       replicationEP.setRequestManager(manager);
98
99       Container container = new Container();
100       container.addEntryPoint(agentEP);
101       container.addEntryPoint(tcpEP);
102       container.addEntryPoint(replicationEP);
103       container.setLifeCycleListener(manager);
104       manager.setContainer(container);
105       container.deploy();
106     }
107
108     tcpServer.start();
109   }
110
111   /**
112    * Stops the service.
113    */

114   public static void stopService() {
115     tcpServer.stop();
116   }
117
118   /**
119    * Returns the default JndiServer id on the local agent server.
120    *
121    * @return the <code>AgentId</code> of the JndiServer
122    */

123   public static AgentId getDefault() {
124     return getDefault(AgentServer.getServerId());
125   }
126
127   /**
128    * Returns the default JndiServer id on the given agent server.
129    *
130    * @param serverId the id of the agent server
131    * @return the <code>AgentId</code> of the JndiServer
132    */

133   public static AgentId getDefault(short serverId) {
134     return new AgentId(
135       serverId, serverId,
136       AgentId.LocalJndiServiceStamp);
137   }
138 }
139
Popular Tags