View Javadoc

1   /*
2    * Copyright (c) 1998-2006 The Jgroup Team.
3    *
4    * This program is free software; you can redistribute it and/or modify
5    * it under the terms of the GNU Lesser General Public License version 2 as
6    * published by the Free Software Foundation.
7    *
8    * This program is distributed in the hope that it will be useful,
9    * but WITHOUT ANY WARRANTY; without even the implied warranty of
10   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11   * GNU Lesser General Public License for more details.
12   *
13   * You should have received a copy of the GNU Lesser General Public License
14   * along with this program; if not, write to the Free Software
15   * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
16   *
17   */
18  
19  package jgroup.util.log;
20  
21  import java.io.File;
22  import java.io.IOException;
23  import java.io.InputStream;
24  import java.net.InetAddress;
25  import java.net.UnknownHostException;
26  import java.util.Calendar;
27  import java.util.logging.LogManager;
28  import java.util.regex.Matcher;
29  import java.util.regex.Pattern;
30  
31  /**
32   * JDK LogManager with support for using system
33   * property references.
34   * <p>
35   * E.g for setting the system property log.dir, 
36   * you can use ${log.dir} in the java logging.properties
37   * file.
38   * 
39   * @author Rohnny Moland
40   */
41  public class JdkLogManager
42    extends LogManager
43  {
44    
45    ////////////////////////////////////////////////////////////////////////////////////////////
46    // Fields
47    ////////////////////////////////////////////////////////////////////////////////////////////
48  
49    /** Buffer holding the property value */
50    private StringBuffer buf;
51    
52    /** Default log dir */
53    private static final String DEFAULT_LOG_DIR = File.separator + "tmp" + File.separator + "jgroup-logs";
54  
55    
56    ////////////////////////////////////////////////////////////////////////////////////////////
57    // Constructor
58    ////////////////////////////////////////////////////////////////////////////////////////////
59  
60    public JdkLogManager()
61    {
62      buf = new StringBuffer();
63    }
64    
65    
66    ////////////////////////////////////////////////////////////////////////////////////////////
67    // Methods
68    ////////////////////////////////////////////////////////////////////////////////////////////
69  
70    /**
71     * Find all ${..} and replace with the system
72     * property if exist.
73     */
74    @Override
75    public String getProperty(String name)
76    {
77      String property = super.getProperty(name);
78      if (property == null)
79        return property;
80      buf.setLength(0);
81      Pattern p = Pattern.compile("\\$\\{[a-zA-Z0-9.]+\\}");
82      Matcher m = p.matcher(property);
83      while (m.find()) {
84        String token = m.group();
85        String sysprop = System.getProperty((token.subSequence(2,token.length()-1)).toString());
86        if (sysprop != null)
87          m.appendReplacement(buf, sysprop);
88      }
89      m.appendTail(buf);
90      return buf.toString();
91    }
92    
93    /**
94     * Create the log directory if it does not
95     * exist.
96     */
97    @Override
98    public void readConfiguration(InputStream in)
99      throws IOException, UnknownHostException
100   {
101     super.readConfiguration(in);
102     System.setProperty("jgroup.log.machine", getMachineName());
103     System.setProperty("jgroup.log.time", getTimeString());
104     String logDir = System.getProperty("jgroup.log.dir", DEFAULT_LOG_DIR);
105     File dir = new File(logDir);
106     if (!dir.exists()) {
107       /* Directory does not exist; create it. */
108       if (!dir.mkdir())
109         throw new IOException("Failed to create log directory");
110     }
111   }
112 
113   /**
114    * @see jgroup.util.Network#getMachineName(String)
115    * (Added here to avoid classloader problems)
116    */
117   private static String getMachineName()
118     throws UnknownHostException
119   {
120     String hostName = InetAddress.getLocalHost().getHostName();
121     int machEndIndex = hostName.indexOf('.');
122     if (machEndIndex == -1)
123       return hostName;
124     else
125       return hostName.substring(0, machEndIndex);
126   }
127   
128   /**
129    * @see jgroup.util.Util#getTimeString()
130    * (Added here to avoid classloader problems)
131    */
132   private static String getTimeString()
133   {
134     Calendar gc = Calendar.getInstance();
135     StringBuilder buffer = new StringBuilder();
136     int hour = gc.get(Calendar.HOUR_OF_DAY);
137     if (hour < 10)
138       buffer.append("0");
139     buffer.append(hour);
140     int minute = gc.get(Calendar.MINUTE);
141     if (minute < 10)
142       buffer.append("0");
143     buffer.append(minute);
144     int second = gc.get(Calendar.SECOND);
145     if (second < 10)
146       buffer.append("0");
147     buffer.append(second);
148     return buffer.toString();
149   }
150   
151 } // END JdkLogManager