/**
* Copyright (C) 2001-2005 France Telecom R&D
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package org.objectweb.util.monolog.wrapper.remote.lib;
import org.objectweb.util.monolog.Monolog;
import org.objectweb.util.monolog.api.Handler;
import org.objectweb.util.monolog.api.Level;
import org.objectweb.util.monolog.api.Logger;
import org.objectweb.util.monolog.api.MonologFactory;
import org.objectweb.util.monolog.api.TopicalLogger;
import org.objectweb.util.monolog.file.monolog.PropertiesConfAccess;
import org.objectweb.util.monolog.wrapper.remote.api.LoggerInfo;
import org.objectweb.util.monolog.wrapper.remote.api.MonologFactoryProxy;
import java.net.MalformedURLException;
import java.net.UnknownHostException;
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
/**
* This class implements the MonologFactoryProxy interface as remote object. It
* permits to configure loggers, handlers and levels on a monolog factory
*
* @author S.Chassande-Barrioz
*/
public class MonologFactoryProxyImpl
extends UnicastRemoteObject
implements MonologFactoryProxy {
private MonologFactory mf;
/**
* Build a remote object managing the default MonologFactory registered
* as current int the Monolog class.
*/
public MonologFactoryProxyImpl() throws RemoteException {
this(Monolog.initialize());
}
/**
* Build a remote object managing a MonologFactory.
* @param mf is the monolog factory instance encapsulated into this remote
* object.
*/
public MonologFactoryProxyImpl(MonologFactory mf) throws RemoteException {
super();
this.mf = mf;
}
/**
* Build a remote object managing a MonologFactory. This object is registerd
* into the RMI registry of the current host.
* @param mf is the monolog factory instance encapsulated into this remote
* object.
* @param rmiName is the name under which this remote object will be
* registered
*/
public MonologFactoryProxyImpl(MonologFactory mf, String rmiName) throws RemoteException {
super();
this.mf = mf;
register(rmiName);
}
/**
* Register this object into the RMI registry of the current host.
* @param name is the name under which this remote object will be
* registered
* @throws RemoteException
*/
public void register(String name) throws RemoteException {
try {
register(java.net.InetAddress.getLocalHost(), name);
} catch (UnknownHostException e) {
throw new RemoteException(e.getMessage());
}
}
/**
* Register this object into a RMI registry.
* @param name is the name under which this remote object will be
* registered
* @param host is the name of the host containing the RMI registry where
* this object has to be registered.
* @throws RemoteException
*/
public void register(java.net.InetAddress host, String name) throws RemoteException {
try {
Naming.rebind("rmi://" + host + "/" + name,this);
} catch (MalformedURLException e) {
throw new RemoteException(e.getMessage());
}
}
public boolean defineLevel(String name, int value) throws RemoteException {
return mf.defineLevel(name, value) != null;
}
public boolean defineLevel(String name, String value)
throws RemoteException {
return mf.defineLevel(name, value) != null;
}
public void removeLevel(String name) throws RemoteException {
mf.removeLevel(name);
}
public Level getLevel(String name) throws RemoteException {
return mf.getLevel(name);
}
public Level getLevel(int value) throws RemoteException {
return mf.getLevel(value);
}
public Level[] getLevels() throws RemoteException {
return mf.getLevels();
}
public int compareTo(String levelname1, String levelname2)
throws RemoteException {
Level l1 = mf.getLevel(levelname1);
if (l1 == null) {
return Integer.MAX_VALUE;
}
Level l2 = mf.getLevel(levelname2);
if (l1 == null) {
return Integer.MIN_VALUE;
}
return l1.compareTo(l2);
}
public boolean createHandler(String hn, String handlertype)
throws RemoteException {
return mf.createHandler(hn, handlertype) != null;
}
public boolean removeHandler(String handlername) throws RemoteException {
return mf.removeHandler(handlername) == null;
}
public String[] getHandlerNames() throws RemoteException {
Handler[] hs = mf.getHandlers();
String[] hns = new String[hs.length];
for(int i=0; i<hs.length; i++) {
hns[i] = hs[i].getName();
}
return hns;
}
public Map getHandlerAttributes(String handlername) throws RemoteException {
Handler h = mf.getHandler(handlername);
String[] ans = h.getAttributeNames();
Map m = new HashMap(ans.length);
for(int i=0; i<ans.length; i++) {
m.put(ans[i], h.getAttribute(ans[i]));
}
return m;
}
public Map getAllHandlerAttributes()
throws RemoteException {
Handler[] hs = mf.getHandlers();
Map m = new HashMap(hs.length);
for(int i=0; i<hs.length; i++) {
m.put(hs[i], getHandlerAttributes(hs[i].getName()));
}
return m;
}
public void setHandlerAttribute(String handlername, String attributeName,
String value) throws RemoteException {
Handler h = mf.getHandler(handlername);
if (h == null) {
throw new RemoteException("No handler '" + handlername + "' found.");
}
h.setAttribute(attributeName, value);
}
public LoggerInfo getLogger(String loggername) throws RemoteException {
return new LoggerInfo((TopicalLogger) mf.getLogger(loggername));
}
public LoggerInfo getLogger(String loggername, String resourceBundleName)
throws RemoteException {
return new LoggerInfo((TopicalLogger)
mf.getLogger(loggername, resourceBundleName));
}
public String getResourceBundleName() throws RemoteException {
return mf.getResourceBundleName();
}
public void setResourceBundleName(String resourceBundleName)
throws RemoteException {
mf.setResourceBundleName(resourceBundleName);
}
public LoggerInfo[] getLoggers() throws RemoteException {
Logger[] ls = mf.getLoggers();
LoggerInfo[] lis = new LoggerInfo[ls.length];
for(int i=0; i<ls.length; i++) {
lis[i] = new LoggerInfo((TopicalLogger) ls[i]);
}
return lis;
}
public void addHandlerToLogger(String handlername, String loggerName)
throws RemoteException {
Handler h = mf.getHandler(handlername);
if (h == null) {
throw new RemoteException("No handler '" + handlername + "' found.");
}
TopicalLogger l = (TopicalLogger) mf.getLogger(loggerName);
try {
l.addHandler(h);
} catch (Exception e) {
throw new RemoteException(e.getMessage());
}
}
public void removeHandlerFromLogger(String handlerName, String loggerName)
throws RemoteException {
Handler h = mf.getHandler(handlerName);
if (h == null) {
return;
}
TopicalLogger l = (TopicalLogger) mf.getLogger(loggerName);
try {
l.removeHandler(h);
} catch (Exception e) {
throw new RemoteException(e.getMessage());
}
}
public void removeAllHandlersFromLogger(String loggerName) throws RemoteException {
TopicalLogger l = (TopicalLogger) mf.getLogger(loggerName);
try {
l.removeAllHandlers();
} catch (Exception e) {
throw new RemoteException(e.getMessage());
}
}
public void setAdditivity(boolean a, String loggerName) throws RemoteException {
TopicalLogger l = (TopicalLogger) mf.getLogger(loggerName);
l.setAdditivity(a);
}
public void setLoggerLevel(int level, String loggerName) throws RemoteException {
TopicalLogger l = (TopicalLogger) mf.getLogger(loggerName);
l.setIntLevel(level);
}
public void setLoggerLevel(String levelName, String loggerName) throws RemoteException {
TopicalLogger l = (TopicalLogger) mf.getLogger(loggerName);
Level level = mf.getLevel(levelName);
if (level != null) {
l.setLevel(level);
}
}
public void addTopicToLogger(String topic, String loggerName)
throws RemoteException {
TopicalLogger l = (TopicalLogger) mf.getLogger(loggerName);
try {
l.addTopic(topic);
} catch (Exception e) {
throw new RemoteException(e.getMessage());
}
}
public void removeTopicFromLogger(String topic, String loggerName)
throws RemoteException {
TopicalLogger l = (TopicalLogger) mf.getLogger(loggerName);
try {
l.removeTopic(topic);
} catch (Exception e) {
throw new RemoteException(e.getMessage());
}
}
public Properties getMonologProperties() throws RemoteException {
Properties p = new Properties();
try {
PropertiesConfAccess.store(p, mf);
} catch (Exception e) {
throw new RemoteException(e.getMessage());
}
return p;
}
public void setMonologProperties(Properties p) throws RemoteException {
try {
PropertiesConfAccess.load(p, mf);
} catch (Exception e) {
throw new RemoteException(e.getMessage());
}
}
}