001    /*
002     * Copyright 2006 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 implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package net.dpml.http;
017    
018    import net.dpml.logging.Logger;
019    
020    import net.dpml.util.ExceptionHelper;
021    
022    /**
023     * Wrapper to redirect Jetty logging to DPML logging.
024     * @author <a href="http://www.dpml.net">Digital Product Meta Library</a>
025     * @version 0.2.0
026     */
027    public class LoggerAdapter implements org.mortbay.log.Logger
028    {    
029        private static Logger m_LOGGER;
030        
031        static void setRootLogger( Logger logger )
032        {
033            if( null == m_LOGGER )
034            {
035                m_LOGGER = logger;
036                System.setProperty( "org.mortbay.log.class", LoggerAdapter.class.getName() );
037                m_LOGGER.debug( "logging adapter established" );
038            }
039            //else
040            //{
041            //    throw new IllegalStateException( "m_LOGGER already initialized." );
042            //}
043        }
044        
045        private final Logger m_logger;
046        
047       /**
048        * Creation of a new logger adapter.
049        */
050        public LoggerAdapter()
051        {
052            if( null == m_LOGGER )
053            {
054                throw new IllegalStateException( "m_LOGGER not initialized." );
055            }
056            else
057            {
058                m_logger = m_LOGGER;
059            }
060        }
061        
062       /**
063        * Creation of a new logger adapter.
064        * @param logger the underlying log channel
065        */
066        LoggerAdapter( Logger logger )
067        {
068            m_logger = logger;
069        }
070        
071       /**
072        * Get the debug enabled status.
073        * @return true if debug is enabled
074        */
075        public boolean isDebugEnabled()
076        {
077            return m_logger.isDebugEnabled();
078        }
079        
080       /**
081        * Set the debug enabled status.
082        * @param flag true if debug is enabled
083        */
084        public void setDebugEnabled( boolean flag )
085        {
086            // ummmm
087        }
088        
089       /**
090        * Publish an info level log message.
091        * @param msg the message
092        * @param arg0 an intial argument
093        * @param arg1 a subsequent argument
094        */
095        public void info( String msg, Object arg0, Object arg1 )
096        {
097            if( m_logger.isInfoEnabled() )
098            {
099                String message = format( msg, arg0, arg1 );
100                m_logger.info( message );
101            }
102        }
103        
104       /**
105        * Publish an debug level log message.
106        * @param message the message
107        * @param cause an exception
108        */
109        public void debug( String message, Throwable cause )
110        {
111            if( isDebugEnabled() )
112            {
113                if( null == cause )
114                {
115                    m_logger.debug( message );
116                }
117                else
118                {
119                    String error = ExceptionHelper.packException( message, cause, false );
120                    m_logger.debug( error );
121                }
122            }
123        }
124        
125       /**
126        * Publish an debug level log message.
127        * @param msg the message
128        * @param arg0 an intial argument
129        * @param arg1 a subsequent argument
130        */
131        public void debug( String msg, Object arg0, Object arg1 )
132        {
133            if( isDebugEnabled() )
134            {
135                String message = format( msg, arg0, arg1 );
136                m_logger.debug( message );
137            }
138        }
139        
140       /**
141        * Publish an warning level log message.
142        * @param msg the message
143        * @param arg0 an intial argument
144        * @param arg1 a subsequent argument
145        */
146        public void warn( String msg, Object arg0, Object arg1 )
147        {
148            if( m_logger.isWarnEnabled() )
149            {
150                String message = format( msg, arg0, arg1 );
151                m_logger.warn( message );
152            }
153        }
154        
155       /**
156        * Publish an warning level log message.
157        * @param message the message
158        * @param error an exception
159        */
160        public void warn( String message, Throwable error )
161        {
162            if( m_logger.isWarnEnabled() )
163            {
164                m_logger.warn( message, error );
165            }
166        }
167    
168        private String format( String msg, Object arg0, Object arg1 )
169        {
170            int i0 = msg.indexOf( "{}" );
171            
172            int i1 = 0;
173            if( i0 < 0 )
174            { 
175                i1 = -1;
176            }
177            else
178            {
179                i1 = msg.indexOf( "{}" , i0 + 2 );
180            }
181            
182            if( ( arg1 != null ) && ( i1 >= 0 ) )
183            {
184                msg = 
185                  msg.substring( 0, i1 ) 
186                  + arg1 
187                  + msg.substring( i1 + 2 );
188            }
189            if( ( arg0 != null ) && ( i0 >= 0 ) )
190            {
191                msg = 
192                  msg.substring( 0, i0 )
193                  + arg0
194                  + msg.substring( i0 + 2 );
195            }
196            return msg;
197        }
198        
199       /**
200        * Create a logger matching the supplied category.
201        * @param category the category name
202        * @return the logging channel
203        */
204        public org.mortbay.log.Logger getLogger( String category )
205        {
206            Logger logger = m_LOGGER.getChildLogger( category );
207            return new LoggerAdapter( logger );
208        }
209        
210        private String trim( String path )
211        {
212            if( path.startsWith( "." ) )
213            {
214                return trim( path.substring( 1 ) );
215            }
216            else if( ".".equals( path ) )
217            {
218                return "";
219            }
220            else
221            {
222                return path;
223            }
224        }
225        
226       /**
227        * Return a string representation of this logger.
228        * @return the string value
229        */
230        public String toString()
231        {
232            return "net.dpml.logging.Logger";
233        }
234    
235    }
236