KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > jk > common > Shm


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.jk.common;
18
19 import java.io.IOException JavaDoc;
20 import java.util.Vector JavaDoc;
21
22 import org.apache.jk.apr.AprImpl;
23 import org.apache.jk.core.Msg;
24 import org.apache.jk.core.MsgContext;
25 import org.apache.jk.core.WorkerEnv;
26 import org.apache.tomcat.util.IntrospectionUtils;
27 import org.apache.tomcat.util.buf.C2BConverter;
28
29 /* The code is a bit confusing at this moment - the class is used as
30    a Bean, or ant Task, or CLI - i.e. you set properties and call execute.
31
32    That's different from the rest of jk handlers wich are stateless ( but
33    similar with Coyote-http ).
34 */

35
36
37 /** Handle the shared memory objects.
38  *
39  * @author Costin Manolache
40  */

41 public class Shm extends JniHandler {
42     String JavaDoc file="/tmp/shm.file";
43     int size;
44     String JavaDoc host="localhost";
45     int port=8009;
46     String JavaDoc unixSocket;
47
48     boolean help=false;
49     boolean unregister=false;
50     boolean reset=false;
51     String JavaDoc dumpFile=null;
52
53     Vector JavaDoc groups=new Vector JavaDoc();
54     
55     // Will be dynamic ( getMethodId() ) after things are stable
56
static final int SHM_WRITE_SLOT=2;
57     static final int SHM_RESET=5;
58     static final int SHM_DUMP=6;
59     
60     public Shm() {
61     }
62
63     /** Scoreboard location
64      */

65     public void setFile( String JavaDoc f ) {
66         file=f;
67     }
68
69     /** Copy the scoreboard in a file for debugging
70      * Will also log a lot of information about what's in the scoreboard.
71      */

72     public void setDump( String JavaDoc dumpFile ) {
73         this.dumpFile=dumpFile;
74     }
75     
76     /** Size. Used only if the scoreboard is to be created.
77      */

78     public void setSize( int size ) {
79         this.size=size;
80     }
81
82     /** Set this to get the scoreboard reset.
83      * The shm segment will be destroyed and a new one created,
84      * with the provided size.
85      *
86      * Requires "file" and "size".
87      */

88     public void setReset(boolean b) {
89         reset=true;
90     }
91
92     /** Ajp13 host
93      */

94     public void setHost( String JavaDoc host ) {
95         this.host=host;
96     }
97
98     /** Mark this instance as belonging to a group
99      */

100     public void setGroup( String JavaDoc grp ) {
101         groups.addElement( grp );
102     }
103
104     /** Ajp13 port
105      */

106     public void setPort( int port ) {
107         this.port=port;
108     }
109
110     /** Unix socket where tomcat is listening.
111      * Use it only if tomcat is on the same host, of course
112      */

113     public void setUnixSocket( String JavaDoc unixSocket ) {
114         this.unixSocket=unixSocket;
115     }
116
117     /** Set this option to mark the tomcat instance as
118         'down', so apache will no longer forward messages to it.
119         Note that requests with a session will still try this
120         host first.
121
122         This can be used to implement gracefull shutdown.
123
124         Host and port are still required, since they are used
125         to identify tomcat.
126     */

127     public void setUnregister( boolean unregister ) {
128         this.unregister=true;
129     }
130     
131     public void init() throws IOException JavaDoc {
132         super.initNative( "shm" );
133         if( apr==null ) return;
134         if( file==null ) {
135             log.error("No shm file, disabling shared memory");
136             apr=null;
137             return;
138         }
139
140         // Set properties and call init.
141
setNativeAttribute( "file", file );
142         if( size > 0 )
143             setNativeAttribute( "size", Integer.toString( size ) );
144         
145         initJkComponent();
146     }
147
148     public void resetScoreboard() throws IOException JavaDoc {
149         if( apr==null ) return;
150         MsgContext mCtx=createMsgContext();
151         Msg msg=(Msg)mCtx.getMsg(0);
152         msg.reset();
153
154         msg.appendByte( SHM_RESET );
155         
156         this.invoke( msg, mCtx );
157     }
158
159     public void dumpScoreboard(String JavaDoc fname) throws IOException JavaDoc {
160         if( apr==null ) return;
161         MsgContext mCtx=createMsgContext();
162         Msg msg=(Msg)mCtx.getMsg(0);
163         C2BConverter c2b=(C2BConverter)mCtx.getNote(C2B_NOTE);
164         msg.reset();
165
166         msg.appendByte( SHM_DUMP );
167
168         appendString( msg, fname, c2b);
169         
170         this.invoke( msg, mCtx );
171     }
172
173     /** Register a tomcat instance
174      * XXX make it more flexible
175      */

176     public void registerTomcat(String JavaDoc host, int port, String JavaDoc unixDomain)
177         throws IOException JavaDoc
178     {
179         String JavaDoc instanceId=host+":" + port;
180
181         String JavaDoc slotName="TOMCAT:" + instanceId;
182         MsgContext mCtx=createMsgContext();
183         Msg msg=(Msg)mCtx.getMsg(0);
184         msg.reset();
185         C2BConverter c2b=(C2BConverter)mCtx.getNote(C2B_NOTE);
186         
187         msg.appendByte( SHM_WRITE_SLOT );
188         appendString( msg, slotName, c2b );
189
190         int channelCnt=1;
191         if( unixDomain != null ) channelCnt++;
192
193         // number of groups. 0 means the default lb.
194
msg.appendInt( groups.size() );
195         for( int i=0; i<groups.size(); i++ ) {
196             appendString( msg, (String JavaDoc)groups.elementAt( i ), c2b);
197             appendString( msg, instanceId, c2b);
198         }
199         
200         // number of channels for this instance
201
msg.appendInt( channelCnt );
202         
203         // The body:
204
appendString(msg, "channel.socket:" + host + ":" + port, c2b );
205         msg.appendInt( 1 );
206         appendString(msg, "tomcatId", c2b);
207         appendString(msg, instanceId, c2b);
208
209         if( unixDomain != null ) {
210             appendString(msg, "channel.apr:" + unixDomain, c2b );
211             msg.appendInt(1);
212             appendString(msg, "tomcatId", c2b);
213             appendString(msg, instanceId, c2b);
214         }
215
216         if (log.isDebugEnabled())
217             log.debug("Register " + instanceId );
218         this.invoke( msg, mCtx );
219     }
220
221     public void unRegisterTomcat(String JavaDoc host, int port)
222         throws IOException JavaDoc
223     {
224         String JavaDoc slotName="TOMCAT:" + host + ":" + port;
225         MsgContext mCtx=createMsgContext();
226         Msg msg=(Msg)mCtx.getMsg(0);
227         msg.reset();
228         C2BConverter c2b=(C2BConverter)mCtx.getNote(C2B_NOTE);
229         
230         msg.appendByte( SHM_WRITE_SLOT );
231         appendString( msg, slotName, c2b );
232
233         // number of channels for this instance
234
msg.appendInt( 0 );
235         msg.appendInt( 0 );
236         
237         if (log.isDebugEnabled())
238             log.debug("UnRegister " + slotName );
239         this.invoke( msg, mCtx );
240     }
241
242     public void destroy() throws IOException JavaDoc {
243         destroyJkComponent();
244     }
245
246     
247     public int invoke(Msg msg, MsgContext ep )
248         throws IOException JavaDoc
249     {
250         if( apr==null ) return 0;
251         log.debug("ChannelShm.invoke: " + ep );
252         super.nativeDispatch( msg, ep, JK_HANDLE_SHM_DISPATCH, 0 );
253         return 0;
254     }
255
256     private static org.apache.commons.logging.Log log=
257         org.apache.commons.logging.LogFactory.getLog( Shm.class );
258
259     
260     //-------------------- Main - use the shm functions from ant or CLI ------
261

262     /** Local initialization - for standalone use
263      */

264     public void initCli() throws IOException JavaDoc {
265         WorkerEnv wEnv=new WorkerEnv();
266         AprImpl apr=new AprImpl();
267         wEnv.addHandler( "apr", apr );
268         wEnv.addHandler( "shm", this );
269         apr.init();
270         if( ! apr.isLoaded() ) {
271             log.error( "No native support. " +
272                        "Make sure libapr.so and libjkjni.so are available in LD_LIBRARY_PATH");
273             return;
274         }
275     }
276     
277     public void execute() {
278         try {
279             if( help ) return;
280             initCli();
281             init();
282
283             if( reset ) {
284                 resetScoreboard();
285             } else if( dumpFile!=null ) {
286                 dumpScoreboard(dumpFile);
287             } else if( unregister ) {
288                 unRegisterTomcat( host, port );
289             } else {
290                 registerTomcat( host, port, unixSocket );
291             }
292         } catch (Exception JavaDoc ex ) {
293             log.error( "Error executing Shm", ex);
294         }
295     }
296
297     public void setHelp( boolean b ) {
298         if (log.isDebugEnabled()) {
299             log.debug("Usage: ");
300             log.debug(" Shm [OPTIONS]");
301             log.debug("");
302             log.debug(" -file SHM_FILE");
303             log.debug(" -group GROUP ( can be specified multiple times )");
304             log.debug(" -host HOST");
305             log.debug(" -port PORT");
306             log.debug(" -unixSocket UNIX_FILE");
307             // log.debug(" -priority XXX");
308
// log.debug(" -lbFactor XXX");
309
}
310         help=true;
311         return;
312     }
313     
314     public static void main( String JavaDoc args[] ) {
315         try {
316             Shm shm=new Shm();
317
318             if( args.length == 0 ||
319                 ( "-?".equals(args[0]) ) ) {
320                 shm.setHelp( true );
321                 return;
322             }
323
324             IntrospectionUtils.processArgs( shm, args);
325             shm.execute();
326         } catch( Exception JavaDoc ex ) {
327             ex.printStackTrace();
328         }
329     }
330 }
331
Popular Tags