001    /* 
002     * Copyright 2005 Stephen McConnell.
003     *
004     * Licensed  under the  Apache License,  Version 2.0  (the "License");
005     * you may not use  this file  except in  compliance with the License.
006     * You may obtain a copy of the License at 
007     * 
008     *   http://www.apache.org/licenses/LICENSE-2.0
009     * 
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed  under the  License is distributed on an "AS IS" BASIS,
012     * WITHOUT  WARRANTIES OR CONDITIONS  OF ANY KIND, either  express  or
013     * implied.
014     * 
015     * See the License for the specific language governing permissions and
016     * limitations under the License.
017     */
018    
019    package net.dpml.depot;
020    
021    import java.io.ByteArrayInputStream;
022    import java.io.ByteArrayOutputStream;
023    import java.util.Properties;
024    import java.util.logging.LogManager;
025    
026    /**
027     * Utility class used to establish the logging configuration for managed subprocesses.
028     * The handler redirects logging records to a remote LoggingService via RMI that 
029     * aggregates logging messages from multiple JVM within a local domain.  This 
030     * configuration handler is declared as the default logging configuration for 
031     * suprocesses launched by the DPML Station.
032     * @author <a href="http://www.dpml.net">Digital Product Meta Library</a>
033     * @version 1.2.0
034     */
035    public class DepotLoggingConfiguration
036    {
037       /**
038        * Creation of the logging controller.
039        */
040        public DepotLoggingConfiguration()
041        {
042            String group = System.getProperty( "dpml.system.group", "root" );
043            String level = getDefaultLevel();
044    
045            Properties properties = new Properties();
046    
047            properties.setProperty( 
048              "handlers", 
049              System.getProperty( 
050                "handlers", 
051                "net.dpml.depot.DepotHandler" ) );
052    
053            //
054            // set the default level by setting the root logger level
055            //
056    
057            properties.setProperty( ".level", level );
058    
059            try
060            {
061                ByteArrayOutputStream out = new ByteArrayOutputStream();
062                properties.store( out, "DPML Logging properties" );
063                byte[] bytes = out.toByteArray();
064                ByteArrayInputStream input = new ByteArrayInputStream( bytes );
065                LogManager manager = LogManager.getLogManager();
066                manager.readConfiguration( input );
067            }
068            catch( Throwable e )
069            {
070                e.printStackTrace();
071            }
072        }
073        
074        private String getDefaultLevel()
075        {
076            if( "true".equals( System.getProperty( "dpml.debug" ) ) )
077            {
078                return "FINE";
079            }
080            else
081            {
082                return System.getProperty( "dpml.logging.level", "INFO" ).toUpperCase();
083            }
084        }
085    }
086