1 16 17 package org.apache.jk.common; 18 19 import java.io.IOException ; 20 import java.util.Vector ; 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 35 36 37 41 public class Shm extends JniHandler { 42 String file="/tmp/shm.file"; 43 int size; 44 String host="localhost"; 45 int port=8009; 46 String unixSocket; 47 48 boolean help=false; 49 boolean unregister=false; 50 boolean reset=false; 51 String dumpFile=null; 52 53 Vector groups=new Vector (); 54 55 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 65 public void setFile( String f ) { 66 file=f; 67 } 68 69 72 public void setDump( String dumpFile ) { 73 this.dumpFile=dumpFile; 74 } 75 76 78 public void setSize( int size ) { 79 this.size=size; 80 } 81 82 88 public void setReset(boolean b) { 89 reset=true; 90 } 91 92 94 public void setHost( String host ) { 95 this.host=host; 96 } 97 98 100 public void setGroup( String grp ) { 101 groups.addElement( grp ); 102 } 103 104 106 public void setPort( int port ) { 107 this.port=port; 108 } 109 110 113 public void setUnixSocket( String unixSocket ) { 114 this.unixSocket=unixSocket; 115 } 116 117 127 public void setUnregister( boolean unregister ) { 128 this.unregister=true; 129 } 130 131 public void init() throws IOException { 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 setNativeAttribute( "file", file ); 142 if( size > 0 ) 143 setNativeAttribute( "size", Integer.toString( size ) ); 144 145 initJkComponent(); 146 } 147 148 public void resetScoreboard() throws IOException { 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 fname) throws IOException { 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 176 public void registerTomcat(String host, int port, String unixDomain) 177 throws IOException 178 { 179 String instanceId=host+":" + port; 180 181 String 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 msg.appendInt( groups.size() ); 195 for( int i=0; i<groups.size(); i++ ) { 196 appendString( msg, (String )groups.elementAt( i ), c2b); 197 appendString( msg, instanceId, c2b); 198 } 199 200 msg.appendInt( channelCnt ); 202 203 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 host, int port) 222 throws IOException 223 { 224 String 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 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 { 243 destroyJkComponent(); 244 } 245 246 247 public int invoke(Msg msg, MsgContext ep ) 248 throws IOException 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 262 264 public void initCli() throws IOException { 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 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 } 310 help=true; 311 return; 312 } 313 314 public static void main( String 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 ex ) { 327 ex.printStackTrace(); 328 } 329 } 330 } 331 | Popular Tags |