KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > util > monolog > wrapper > remote > lib > MonologFactoryProxyImpl


1 /**
2  * Copyright (C) 2001-2005 France Telecom R&D
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */

18 package org.objectweb.util.monolog.wrapper.remote.lib;
19
20 import org.objectweb.util.monolog.Monolog;
21 import org.objectweb.util.monolog.api.Handler;
22 import org.objectweb.util.monolog.api.Level;
23 import org.objectweb.util.monolog.api.Logger;
24 import org.objectweb.util.monolog.api.MonologFactory;
25 import org.objectweb.util.monolog.api.TopicalLogger;
26 import org.objectweb.util.monolog.file.monolog.PropertiesConfAccess;
27 import org.objectweb.util.monolog.wrapper.remote.api.LoggerInfo;
28 import org.objectweb.util.monolog.wrapper.remote.api.MonologFactoryProxy;
29
30 import java.net.MalformedURLException JavaDoc;
31 import java.net.UnknownHostException JavaDoc;
32 import java.rmi.Naming JavaDoc;
33 import java.rmi.RemoteException JavaDoc;
34 import java.rmi.server.UnicastRemoteObject JavaDoc;
35 import java.util.HashMap JavaDoc;
36 import java.util.Map JavaDoc;
37 import java.util.Properties JavaDoc;
38
39 /**
40  * This class implements the MonologFactoryProxy interface as remote object. It
41  * permits to configure loggers, handlers and levels on a monolog factory
42  *
43  * @author S.Chassande-Barrioz
44  */

45 public class MonologFactoryProxyImpl
46     extends UnicastRemoteObject JavaDoc
47     implements MonologFactoryProxy {
48
49     private MonologFactory mf;
50     
51     /**
52      * Build a remote object managing the default MonologFactory registered
53      * as current int the Monolog class.
54      */

55     public MonologFactoryProxyImpl() throws RemoteException JavaDoc {
56         this(Monolog.initialize());
57     }
58
59     /**
60      * Build a remote object managing a MonologFactory.
61      * @param mf is the monolog factory instance encapsulated into this remote
62      * object.
63      */

64     public MonologFactoryProxyImpl(MonologFactory mf) throws RemoteException JavaDoc {
65         super();
66         this.mf = mf;
67     }
68     
69     /**
70      * Build a remote object managing a MonologFactory. This object is registerd
71      * into the RMI registry of the current host.
72      * @param mf is the monolog factory instance encapsulated into this remote
73      * object.
74      * @param rmiName is the name under which this remote object will be
75      * registered
76      */

77     public MonologFactoryProxyImpl(MonologFactory mf, String JavaDoc rmiName) throws RemoteException JavaDoc {
78         super();
79         this.mf = mf;
80         register(rmiName);
81     }
82     
83     /**
84      * Register this object into the RMI registry of the current host.
85      * @param name is the name under which this remote object will be
86      * registered
87      * @throws RemoteException
88      */

89     public void register(String JavaDoc name) throws RemoteException JavaDoc {
90         try {
91             register(java.net.InetAddress.getLocalHost(), name);
92         } catch (UnknownHostException JavaDoc e) {
93             throw new RemoteException JavaDoc(e.getMessage());
94         }
95     }
96     
97     /**
98      * Register this object into a RMI registry.
99      * @param name is the name under which this remote object will be
100      * registered
101      * @param host is the name of the host containing the RMI registry where
102      * this object has to be registered.
103      * @throws RemoteException
104      */

105     public void register(java.net.InetAddress JavaDoc host, String JavaDoc name) throws RemoteException JavaDoc {
106         try {
107             Naming.rebind("rmi://" + host + "/" + name,this);
108         } catch (MalformedURLException JavaDoc e) {
109             throw new RemoteException JavaDoc(e.getMessage());
110         }
111     }
112     
113     public boolean defineLevel(String JavaDoc name, int value) throws RemoteException JavaDoc {
114         return mf.defineLevel(name, value) != null;
115     }
116
117     public boolean defineLevel(String JavaDoc name, String JavaDoc value)
118             throws RemoteException JavaDoc {
119         return mf.defineLevel(name, value) != null;
120     }
121
122     public void removeLevel(String JavaDoc name) throws RemoteException JavaDoc {
123         mf.removeLevel(name);
124     }
125
126     public Level getLevel(String JavaDoc name) throws RemoteException JavaDoc {
127         return mf.getLevel(name);
128     }
129
130     public Level getLevel(int value) throws RemoteException JavaDoc {
131         return mf.getLevel(value);
132     }
133
134     public Level[] getLevels() throws RemoteException JavaDoc {
135         return mf.getLevels();
136     }
137
138     public int compareTo(String JavaDoc levelname1, String JavaDoc levelname2)
139             throws RemoteException JavaDoc {
140         Level l1 = mf.getLevel(levelname1);
141         if (l1 == null) {
142             return Integer.MAX_VALUE;
143         }
144         Level l2 = mf.getLevel(levelname2);
145         if (l1 == null) {
146             return Integer.MIN_VALUE;
147         }
148         return l1.compareTo(l2);
149     }
150
151     public boolean createHandler(String JavaDoc hn, String JavaDoc handlertype)
152             throws RemoteException JavaDoc {
153         return mf.createHandler(hn, handlertype) != null;
154     }
155
156     public boolean removeHandler(String JavaDoc handlername) throws RemoteException JavaDoc {
157         return mf.removeHandler(handlername) == null;
158     }
159
160     public String JavaDoc[] getHandlerNames() throws RemoteException JavaDoc {
161         Handler[] hs = mf.getHandlers();
162         String JavaDoc[] hns = new String JavaDoc[hs.length];
163         for(int i=0; i<hs.length; i++) {
164             hns[i] = hs[i].getName();
165         }
166         return hns;
167     }
168
169     public Map JavaDoc getHandlerAttributes(String JavaDoc handlername) throws RemoteException JavaDoc {
170         Handler h = mf.getHandler(handlername);
171         String JavaDoc[] ans = h.getAttributeNames();
172         Map JavaDoc m = new HashMap JavaDoc(ans.length);
173         for(int i=0; i<ans.length; i++) {
174             m.put(ans[i], h.getAttribute(ans[i]));
175         }
176         return m;
177     }
178
179     public Map JavaDoc getAllHandlerAttributes()
180             throws RemoteException JavaDoc {
181         Handler[] hs = mf.getHandlers();
182         Map JavaDoc m = new HashMap JavaDoc(hs.length);
183         for(int i=0; i<hs.length; i++) {
184             m.put(hs[i], getHandlerAttributes(hs[i].getName()));
185         }
186         return m;
187     }
188
189     public void setHandlerAttribute(String JavaDoc handlername, String JavaDoc attributeName,
190             String JavaDoc value) throws RemoteException JavaDoc {
191         Handler h = mf.getHandler(handlername);
192         if (h == null) {
193             throw new RemoteException JavaDoc("No handler '" + handlername + "' found.");
194         }
195         h.setAttribute(attributeName, value);
196     }
197
198     public LoggerInfo getLogger(String JavaDoc loggername) throws RemoteException JavaDoc {
199         return new LoggerInfo((TopicalLogger) mf.getLogger(loggername));
200     }
201
202     public LoggerInfo getLogger(String JavaDoc loggername, String JavaDoc resourceBundleName)
203             throws RemoteException JavaDoc {
204         return new LoggerInfo((TopicalLogger)
205                 mf.getLogger(loggername, resourceBundleName));
206     }
207
208     public String JavaDoc getResourceBundleName() throws RemoteException JavaDoc {
209         return mf.getResourceBundleName();
210     }
211
212     public void setResourceBundleName(String JavaDoc resourceBundleName)
213             throws RemoteException JavaDoc {
214         mf.setResourceBundleName(resourceBundleName);
215     }
216
217     public LoggerInfo[] getLoggers() throws RemoteException JavaDoc {
218         Logger[] ls = mf.getLoggers();
219         LoggerInfo[] lis = new LoggerInfo[ls.length];
220         for(int i=0; i<ls.length; i++) {
221             lis[i] = new LoggerInfo((TopicalLogger) ls[i]);
222         }
223         return lis;
224     }
225
226     public void addHandlerToLogger(String JavaDoc handlername, String JavaDoc loggerName)
227             throws RemoteException JavaDoc {
228         Handler h = mf.getHandler(handlername);
229         if (h == null) {
230             throw new RemoteException JavaDoc("No handler '" + handlername + "' found.");
231         }
232         TopicalLogger l = (TopicalLogger) mf.getLogger(loggerName);
233         try {
234             l.addHandler(h);
235         } catch (Exception JavaDoc e) {
236             throw new RemoteException JavaDoc(e.getMessage());
237         }
238     }
239
240     public void removeHandlerFromLogger(String JavaDoc handlerName, String JavaDoc loggerName)
241             throws RemoteException JavaDoc {
242         Handler h = mf.getHandler(handlerName);
243         if (h == null) {
244             return;
245         }
246         TopicalLogger l = (TopicalLogger) mf.getLogger(loggerName);
247         try {
248             l.removeHandler(h);
249         } catch (Exception JavaDoc e) {
250             throw new RemoteException JavaDoc(e.getMessage());
251         }
252     }
253
254     public void removeAllHandlersFromLogger(String JavaDoc loggerName) throws RemoteException JavaDoc {
255         TopicalLogger l = (TopicalLogger) mf.getLogger(loggerName);
256         try {
257             l.removeAllHandlers();
258         } catch (Exception JavaDoc e) {
259             throw new RemoteException JavaDoc(e.getMessage());
260         }
261     }
262
263     public void setAdditivity(boolean a, String JavaDoc loggerName) throws RemoteException JavaDoc {
264         TopicalLogger l = (TopicalLogger) mf.getLogger(loggerName);
265         l.setAdditivity(a);
266     }
267
268     public void setLoggerLevel(int level, String JavaDoc loggerName) throws RemoteException JavaDoc {
269         TopicalLogger l = (TopicalLogger) mf.getLogger(loggerName);
270         l.setIntLevel(level);
271     }
272
273     public void setLoggerLevel(String JavaDoc levelName, String JavaDoc loggerName) throws RemoteException JavaDoc {
274         TopicalLogger l = (TopicalLogger) mf.getLogger(loggerName);
275         Level level = mf.getLevel(levelName);
276         if (level != null) {
277             l.setLevel(level);
278         }
279     }
280
281     public void addTopicToLogger(String JavaDoc topic, String JavaDoc loggerName)
282             throws RemoteException JavaDoc {
283         TopicalLogger l = (TopicalLogger) mf.getLogger(loggerName);
284         try {
285             l.addTopic(topic);
286         } catch (Exception JavaDoc e) {
287             throw new RemoteException JavaDoc(e.getMessage());
288         }
289     }
290
291     public void removeTopicFromLogger(String JavaDoc topic, String JavaDoc loggerName)
292             throws RemoteException JavaDoc {
293         TopicalLogger l = (TopicalLogger) mf.getLogger(loggerName);
294         try {
295             l.removeTopic(topic);
296         } catch (Exception JavaDoc e) {
297             throw new RemoteException JavaDoc(e.getMessage());
298         }
299     }
300
301     public Properties JavaDoc getMonologProperties() throws RemoteException JavaDoc {
302         Properties JavaDoc p = new Properties JavaDoc();
303         try {
304             PropertiesConfAccess.store(p, mf);
305         } catch (Exception JavaDoc e) {
306             throw new RemoteException JavaDoc(e.getMessage());
307         }
308         return p;
309     }
310     
311     public void setMonologProperties(Properties JavaDoc p) throws RemoteException JavaDoc {
312         try {
313             PropertiesConfAccess.load(p, mf);
314         } catch (Exception JavaDoc e) {
315             throw new RemoteException JavaDoc(e.getMessage());
316         }
317     }
318
319 }
320
Popular Tags