|
Glassfish example source code file (JkMain.java)
The Glassfish JkMain.java source code/* * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. * * Copyright (c) 1997-2010 Oracle and/or its affiliates. All rights reserved. * * The contents of this file are subject to the terms of either the GNU * General Public License Version 2 only ("GPL") or the Common Development * and Distribution License("CDDL") (collectively, the "License"). You * may not use this file except in compliance with the License. You can * obtain a copy of the License at * https://glassfish.dev.java.net/public/CDDL+GPL_1_1.html * or packager/legal/LICENSE.txt. See the License for the specific * language governing permissions and limitations under the License. * * When distributing the software, include this License Header Notice in each * file and include the License file at packager/legal/LICENSE.txt. * * GPL Classpath Exception: * Oracle designates this particular file as subject to the "Classpath" * exception as provided by Oracle in the GPL Version 2 section of the License * file that accompanied this code. * * Modifications: * If applicable, add the following below the License Header, with the fields * enclosed by brackets [] replaced by your own identifying information: * "Portions Copyright [year] [name of copyright owner]" * * Contributor(s): * If you wish your version of this file to be governed by only the CDDL or * only the GPL Version 2, indicate your decision by adding "[Contributor] * elects to include this software in this distribution under the [CDDL or GPL * Version 2] license." If you don't indicate a single choice of license, a * recipient has the option to distribute your version of this file under * either the CDDL, the GPL Version 2 or to extend the choice of license to * its licensees as provided above. However, if you add GPL Version 2 code * and therefore, elected the GPL Version 2 license, then the option applies * only if the new code is made subject to such option by the copyright * holder. * * * This file incorporates work covered by the following copyright and * permission notice: * * Copyright 2004 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.jk.server; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.PrintStream; import java.util.Enumeration; import java.util.Hashtable; import java.util.Properties; import java.util.StringTokenizer; import java.util.Vector; import java.util.logging.*; import javax.management.MBeanRegistration; import javax.management.MBeanServer; import javax.management.ObjectName; import org.apache.jk.core.JkHandler; import org.apache.jk.core.WorkerEnv; import org.apache.tomcat.util.modeler.Registry; import com.sun.grizzly.util.IntrospectionUtils; /** Main class used to startup and configure jk. It manages the conf/jk2.properties file * and is the target of JMX proxy. * * It implements a policy of save-on-change - whenever a property is changed at * runtime the jk2.properties file will be overriden. * * You can edit the config file when tomcat is stoped ( or if you don't use JMX or * other admin tools ). * * The format of jk2.properties: * <dl> * <dt>TYPE[.LOCALNAME].PROPERTY_NAME=VALUE * <dd>Set a property on the associated component. TYPE will be used to * find the class name and instantiate the component. LOCALNAME allows * multiple instances. In JMX mode, TYPE and LOCALNAME will form the * JMX name ( eventually combined with a 'jk2' component ) * * <dt>NAME=VALUE * <dd>Define global properties to be used in ${} substitutions * * <dt>class.COMPONENT_TYPE=JAVA_CLASS_NAME * <dd>Adds a new 'type' of component. We predefine all known types. * </dl> * * Instances are created the first time a component name is found. In addition, * 'handler.list' property will override the list of 'default' components that are * loaded automatically. * * Note that the properties file is just one (simplistic) way to configure jk. We hope * to see configs based on registry, LDAP, db, etc. ( XML is not necesarily better ) * * @author Costin Manolache */ @SuppressWarnings({"StringContatenationInLoop"}) public class JkMain implements MBeanRegistration { WorkerEnv wEnv; String propFile; Properties props=new Properties(); Properties modules=new Properties(); boolean modified=false; boolean started=false; boolean saveProperties=false; public JkMain() { modules.put("channelSocket", "org.apache.jk.common.ChannelSocket"); modules.put("channelNioSocket", "org.apache.jk.common.ChannelNioSocket"); modules.put("channelUnix", "org.apache.jk.common.ChannelUn"); modules.put("channelJni", "org.apache.jk.common.ChannelJni"); modules.put("apr", "org.apache.jk.apr.AprImpl"); modules.put("mx", "org.apache.jk.common.JkMX"); modules.put("modeler", "org.apache.jk.common.JkModeler"); modules.put("shm", "org.apache.jk.common.Shm"); modules.put("request","org.apache.jk.common.HandlerRequest"); modules.put("container","org.apache.jk.common.HandlerRequest"); modules.put("modjk","org.apache.jk.common.ModJkMX"); } private static final String DEFAULT_HTTPS="com.sun.net.ssl.internal.www.protocol"; private void initHTTPSUrls() { try { // 11657: if only ajp is used, https: redirects need to work ( at least for 1.3+) String value = System.getProperty("java.protocol.handler.pkgs"); if (value == null) { value = DEFAULT_HTTPS; } else if (value.indexOf(DEFAULT_HTTPS) >= 0 ) { return; // already set } else { value += "|" + DEFAULT_HTTPS; } System.setProperty("java.protocol.handler.pkgs", value); } catch(Exception ex ) { log.log(Level.INFO, "Error adding SSL Protocol Handler",ex); } } // -------------------- Setting -------------------- /** Load a .properties file into and set the values * into jk2 configuration. */ public void setPropertiesFile( String p ) { propFile=p; if( started ) { loadPropertiesFile(); } } public String getPropertiesFile() { return propFile; } public void setSaveProperties( boolean b ) { saveProperties=b; } /** Set a name/value as a jk2 property */ public void setProperty( String n, String v ) { if( "jkHome".equals( n ) ) { setJkHome( v ); } if( "propertiesFile".equals( n ) ) { setPropertiesFile( v ); } props.put( n, v ); if( started ) { processProperty( n, v ); saveProperties(); } } /** * Retrieve a property. */ public Object getProperty(String name) { String alias = replacements.get(name); Object result = null; if(alias != null) { result = props.get(alias); } if(result == null) { result = props.get(name); } return result; } /** * Set the <code>channelClassName that will used to connect to * httpd. */ public void setChannelClassName(String name) { props.put( "handler.channel.className",name); } public String getChannelClassName() { return props.getProperty( "handler.channel.className"); } /** * Set the <code>workerClassName that will handle the request. * ( sort of 'pivot' in axis :-) */ public void setWorkerClassName(String name) { props.put( "handler.container.className",name); } public String getWorkerClassName() { return props.getProperty( "handler.container.className"); } /** Set the base dir of jk2. ( including WEB-INF if in a webapp ). * We'll try to guess it from classpath if none is set ( for * example on command line ), but if in a servlet environment * you need to use Context.getRealPath or a system property or * set it expliciltey. */ public void setJkHome( String s ) { getWorkerEnv().setJkHome(s); } public String getJkHome() { return getWorkerEnv().getJkHome(); } String out; String err; File propsF; public void setOut( String s ) { out =s; } public String getOut() { return out; } public void setErr( String s ) { err =s; } public String getErr() { return err; } // -------------------- Initialization -------------------- public void init() throws IOException { long t1=System.currentTimeMillis(); if(null != out) { PrintStream outS=new PrintStream(new FileOutputStream(out)); System.setOut(outS); } if(null != err) { PrintStream errS=new PrintStream(new FileOutputStream(err)); System.setErr(errS); } String home=getWorkerEnv().getJkHome(); if( home==null ) { // XXX use IntrospectionUtil to find myself guessHome(); } home=getWorkerEnv().getJkHome(); if( home==null ) { log.info( "Can't find home, jk2.properties not loaded"); } if(log.isLoggable(Level.FINEST)) log.finest("Starting Jk2, base dir= " + home ); loadPropertiesFile(); String initHTTPS = props.getProperty("class.initHTTPS"); if("true".equalsIgnoreCase(initHTTPS)) { initHTTPSUrls(); } long t2=System.currentTimeMillis(); initTime=t2-t1; } static final String defaultHandlers[]= { "request", "container", "channelSocket"}; /* static String defaultHandlers[]= { "apr", "shm", "request", "container", "channelSocket", "channelJni", "channelUnix"}; */ public void stop() { for( int i=0; i<wEnv.getHandlerCount(); i++ ) { if( wEnv.getHandler(i) != null ) { try { wEnv.getHandler(i).destroy(); } catch( IOException ex) { log.log(Level.SEVERE, "Error stopping " + wEnv.getHandler(i).getName(), ex); } } } started=false; } public void start() throws IOException { long t1=System.currentTimeMillis(); // We must have at least 3 handlers: // channel is the 'transport' // request is the request processor or 'global' chain // container is the 'provider' // Additional handlers may exist and be used internally // or be chained to create one of the standard handlers String handlers[]=defaultHandlers; // backward compat String workers=props.getProperty( "handler.list", null ); if( workers!=null ) { handlers= split( workers, ","); } // Load additional component declarations processModules(); for (String name : handlers) { JkHandler w = getWorkerEnv().getHandler(name); if (w == null) { newHandler(name, "", name); } } // Process properties - and add aditional handlers. processProperties(); for( int i=0; i<wEnv.getHandlerCount(); i++ ) { if( wEnv.getHandler(i) != null ) { try { wEnv.getHandler(i).init(); } catch( IOException ex) { if( "apr".equals(wEnv.getHandler(i).getName() )) { log.info( "APR not loaded, disabling jni components: " + ex.toString()); } else { log.log(Level.SEVERE, "error initializing " + wEnv.getHandler(i).getName(), ex ); } } } } started=true; long t2=System.currentTimeMillis(); startTime=t2-t1; saveProperties(); log.info("Jk running ID=" + wEnv.getLocalId() + " time=" + initTime + "/" + startTime + " config=" + propFile); } // -------------------- Usefull methods -------------------- public WorkerEnv getWorkerEnv() { if( wEnv==null ) { wEnv=new WorkerEnv(); } return wEnv; } public void setWorkerEnv(WorkerEnv wEnv) { this.wEnv = wEnv; } /* A bit of magic to support workers.properties without giving up the clean get/set */ public void setBeanProperty( Object target, String name, String val ) { if( val!=null ) val=IntrospectionUtils.replaceProperties( val, (Hashtable)props, null ); if( log.isLoggable(Level.FINEST)) log.finest( "setProperty " + target + " " + name + "=" + val ); IntrospectionUtils.setProperty( target, name, val ); } /* * Set a handler property */ public void setPropertyString( String handlerN, String name, String val ) { if( log.isLoggable(Level.FINEST) ) log.finest( "setProperty " + handlerN + " " + name + "=" + val ); Object target=getWorkerEnv().getHandler( handlerN ); setBeanProperty( target, name, val ); if( started ) { saveProperties(); } } /** The time it took to initialize jk ( ms) */ public long getInitTime() { return initTime; } /** The time it took to start jk ( ms ) */ public long getStartTime() { return startTime; } // -------------------- Main -------------------- long initTime; long startTime; public static void main(String args[]) { try { if( args.length == 1 && ( "-?".equals(args[0]) || "-h".equals( args[0])) ) { System.out.println("Usage: "); System.out.println(" JkMain [args]"); System.out.println(); System.out.println(" Each bean setter corresponds to an arg ( like -debug 10 )"); System.out.println(" System properties:"); System.out.println(" jk2.home Base dir of jk2"); return; } JkMain jkMain=new JkMain(); IntrospectionUtils.processArgs( jkMain, args, new String[] {}, null, new Hashtable<String,String>()); jkMain.init(); jkMain.start(); } catch( Exception ex ) { log.log(Level.WARNING, "Error running",ex); } } // -------------------- Private methods -------------------- private boolean checkPropertiesFile() { if(propFile == null) { return false; } propsF = new File(propFile); if(!propsF.isAbsolute()) { String home = getWorkerEnv().getJkHome(); if( home == null ) { return false; } propsF = new File(home, propFile); } return propsF.exists(); } private void loadPropertiesFile() { if(!checkPropertiesFile()) { return; } try { props.load( new FileInputStream(propsF) ); } catch(IOException ex ){ log.log(Level.WARNING, "Unable to load properties from "+propsF,ex); } } public void saveProperties() { if( !saveProperties) return; if(propsF == null) { log.warning("No properties file specified. Unable to save"); return; } // Temp - to check if it works File outFile= new File(propsF.getParentFile(), propsF.getName()+".save"); log.finest("Saving properties " + outFile ); try { props.store( new FileOutputStream(outFile), "AUTOMATICALLY GENERATED" ); } catch(IOException ex ){ log.log(Level.WARNING, "Unable to save to "+outFile,ex); } } // translate top-level keys ( from coyote or generic ) into component keys static final Hashtable<String, String> replacements=new Hashtable Other Glassfish examples (source code examples)Here is a short list of links related to this Glassfish JkMain.java source code file: |
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.