r1197 - in xwiki-clients/p2pxwiki/src: main/java/com/xpn/p2pxwiki main/java/com/xpn/p2pxwiki/communication main/java/com/xpn/p2pxwiki/communication/jxta main/java/com/xpn/p2pxwiki/communication/rpc main/java/com/xpn/p2pxwiki/services main/java/com/xpn/p2pxwiki/services/file main/java/com/xpn/p2pxwiki/utils test/java/com/xpn/p2pxwiki/communication
Sergiu Dumitriu
sdumitriu at users.forge.objectweb.org
Wed Aug 23 11:33:11 CEST 2006
Author: sdumitriu
Date: 2006-08-23 11:32:19 +0200 (Wed, 23 Aug 2006)
New Revision: 1197
Added:
xwiki-clients/p2pxwiki/src/main/java/com/xpn/p2pxwiki/utils/P2PUtil.java
Modified:
xwiki-clients/p2pxwiki/src/main/java/com/xpn/p2pxwiki/P2PXWikiException.java
xwiki-clients/p2pxwiki/src/main/java/com/xpn/p2pxwiki/communication/ConnectorPlugin.java
xwiki-clients/p2pxwiki/src/main/java/com/xpn/p2pxwiki/communication/jxta/JxtaHandlerStub.java
xwiki-clients/p2pxwiki/src/main/java/com/xpn/p2pxwiki/communication/rpc/RpcHandlerStub.java
xwiki-clients/p2pxwiki/src/main/java/com/xpn/p2pxwiki/services/P2PServicesPlugin.java
xwiki-clients/p2pxwiki/src/main/java/com/xpn/p2pxwiki/services/file/FileGroup.java
xwiki-clients/p2pxwiki/src/main/java/com/xpn/p2pxwiki/services/file/FileP2PServer.java
xwiki-clients/p2pxwiki/src/main/java/com/xpn/p2pxwiki/services/file/FileWikiGroupServices.java
xwiki-clients/p2pxwiki/src/test/java/com/xpn/p2pxwiki/communication/AbstractLocalTest.java
xwiki-clients/p2pxwiki/src/test/java/com/xpn/p2pxwiki/communication/RpcLocalTest.java
Log:
Minor refactoring of XWikiP2P.
Modified: xwiki-clients/p2pxwiki/src/main/java/com/xpn/p2pxwiki/P2PXWikiException.java
===================================================================
--- xwiki-clients/p2pxwiki/src/main/java/com/xpn/p2pxwiki/P2PXWikiException.java 2006-08-23 09:08:38 UTC (rev 1196)
+++ xwiki-clients/p2pxwiki/src/main/java/com/xpn/p2pxwiki/P2PXWikiException.java 2006-08-23 09:32:19 UTC (rev 1197)
@@ -4,26 +4,34 @@
public class P2PXWikiException extends PluginException {
private static final long serialVersionUID = 1106138323477622364L;
- public static final int ERROR_P2PXWIKI_COMMUNICATION = 30999;
- public static final int ERROR_P2PXWIKI_REMOTECALL = 30998;
- public static final int ERROR_P2PXWIKI_NO_SUCH_GROUP = 30997;
- public static final int ERROR_P2PXWIKI_ACCESSDENIED = 30996;
- public static final int ERROR_P2PXWIKI_GROUP_NOTALLOWED = 30995;
- static String module = "P2PXWiki";
-
+
+ public static final int COMMUNICATION = 30999;
+ public static final int REMOTE_CALL = 30998;
+ public static final int NO_SUCH_GROUP = 30997;
+ public static final int ACCESS_DENIED = 30996;
+ public static final int GROUP_NOT_ALLOWED = 30995;
+ public static final int INSTANCE = 30994;
+ public static final int SINGLETON_INSTANCE = 30993;
+
+ static final String MODULE = "P2PXWiki";
+
public P2PXWikiException(int code, String message, Throwable e,
Object[] args) {
- super(module, code, message, e, args);
+ super(MODULE, code, message, e, args);
}
public P2PXWikiException(int code, String message, Throwable e) {
- super(module, code, message, e, null);
+ super(MODULE, code, message, e, null);
}
public P2PXWikiException(int code, String message) {
- super(module, code, message, null, null);
+ super(MODULE, code, message, null, null);
}
+ public P2PXWikiException(int code, Throwable e) {
+ super(MODULE, code, "Wrapping exception", e, null);
+ }
+
public P2PXWikiException() {
super();
}
Modified: xwiki-clients/p2pxwiki/src/main/java/com/xpn/p2pxwiki/communication/ConnectorPlugin.java
===================================================================
--- xwiki-clients/p2pxwiki/src/main/java/com/xpn/p2pxwiki/communication/ConnectorPlugin.java 2006-08-23 09:08:38 UTC (rev 1196)
+++ xwiki-clients/p2pxwiki/src/main/java/com/xpn/p2pxwiki/communication/ConnectorPlugin.java 2006-08-23 09:32:19 UTC (rev 1197)
@@ -29,14 +29,14 @@
package com.xpn.p2pxwiki.communication;
-import java.lang.reflect.InvocationTargetException;
-
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
+import com.xpn.p2pxwiki.P2PXWikiException;
import com.xpn.p2pxwiki.communication.jxta.JxtaConnectionFactory;
import com.xpn.p2pxwiki.communication.jxta.JxtaHandlerManager;
import com.xpn.p2pxwiki.communication.jxta.JxtaNetwork;
+import com.xpn.p2pxwiki.utils.P2PUtil;
import com.xpn.xwiki.XWiki;
import com.xpn.xwiki.XWikiContext;
import com.xpn.xwiki.XWikiException;
@@ -74,6 +74,7 @@
private static HandlerManager handlerManager;
private static String networkType;
private static String connectorType;
+ private static String handlerManagerType;
private static int cacheTime;
private static String peerName;
private static int serverPort;
@@ -131,63 +132,39 @@
networkType = xwiki.Param(NETWORK_TYPE, DEFAULT_NETWORK_TYPE);
// TODO: Retry with default network on failure, if this isn't already the default
try{
- network = (Network)Class.forName(networkType).getMethod("getInstance", (Class[])null).invoke(null, (Object[])null);
+ network = (Network)P2PUtil.getSingletonInstance(networkType);
}
- catch(ClassNotFoundException ex){
- log.error("Cannot load network type", ex);
+ catch(P2PXWikiException ex){
+ log.error("Cannot load network", ex);
+ // we continue and hope for the best
}
- catch(NoSuchMethodException ex){
- log.error("Not a network type", ex);
- }
- catch(InvocationTargetException ex){
- log.error("Something fishy going on...", ex);
- }
- catch(IllegalAccessException ex){
- log.error("Something fishy going on...", ex);
- }
log.debug("Obtained network: " + network.getClass().getCanonicalName());
log.debug("Configuring connector factory");
connectorType = xwiki.Param(CONNECTOR_FACTORY, DEFAULT_CONNECTOR_FACTORY);
// TODO: Retry with default connector on failure, if this isn't already the default
try{
- factory = (ConnectionFactory)Class.forName(connectorType)
- .getMethod("getInstance", (Class[])null).invoke(null, (Object[])null);
+ factory = (ConnectionFactory)P2PUtil.getSingletonInstance(connectorType);
}
- catch(ClassNotFoundException ex){
+ catch(P2PXWikiException ex){
log.error("Cannot load connector factory", ex);
+ // we continue and hope for the best
}
- catch(NoSuchMethodException ex){
- log.error("Not a connector factory", ex);
- }
- catch(InvocationTargetException ex){
- log.error("Something fishy going on...", ex);
- }
- catch(IllegalAccessException ex){
- log.error("Something fishy going on...", ex);
- }
+
log.debug("Obtained factory: " + factory.getClass().getCanonicalName());
log.debug("Configuring handler manager");
// TODO: Retry with default manager on failure, if this isn't already the default
- connectorType = xwiki.Param(HANDLER_MANAGER, DEFAULT_HANDLER_MANAGER);
+ handlerManagerType = xwiki.Param(HANDLER_MANAGER, DEFAULT_HANDLER_MANAGER);
try{
- handlerManager = (HandlerManager)Class.forName(connectorType)
- .getMethod("getInstance", (Class[])null).invoke(null, (Object[])null);
+ handlerManager = (HandlerManager)P2PUtil.getSingletonInstance(handlerManagerType);
}
- catch(ClassNotFoundException ex){
+ catch(P2PXWikiException ex){
log.error("Cannot load handler manager", ex);
+ // we continue and hope for the best
}
- catch(NoSuchMethodException ex){
- log.error("Not a handler manager", ex);
- }
- catch(InvocationTargetException ex){
- log.error("Something fishy going on...", ex);
- }
- catch(IllegalAccessException ex){
- log.error("Something fishy going on...", ex);
- }
- log.debug("Obtained factory: " + factory.getClass().getCanonicalName());
+
+ log.debug("Obtained handler manager: " + handlerManager.getClass().getCanonicalName());
}
/**
Modified: xwiki-clients/p2pxwiki/src/main/java/com/xpn/p2pxwiki/communication/jxta/JxtaHandlerStub.java
===================================================================
--- xwiki-clients/p2pxwiki/src/main/java/com/xpn/p2pxwiki/communication/jxta/JxtaHandlerStub.java 2006-08-23 09:08:38 UTC (rev 1196)
+++ xwiki-clients/p2pxwiki/src/main/java/com/xpn/p2pxwiki/communication/jxta/JxtaHandlerStub.java 2006-08-23 09:32:19 UTC (rev 1197)
@@ -35,10 +35,10 @@
return connection.execute(function, args);
}
catch(XmlRpcException ex){
- throw new P2PXWikiException(P2PXWikiException.ERROR_P2PXWIKI_REMOTECALL, "Cannot execute remote call because of an unhandled RPC exception", ex);
+ throw new P2PXWikiException(P2PXWikiException.REMOTE_CALL, "Cannot execute remote call because of an unhandled RPC exception", ex);
}
catch(IOException ex){
- throw new P2PXWikiException(P2PXWikiException.ERROR_P2PXWIKI_COMMUNICATION, "Cannot execute remote call because of an unhandled IO exception", ex);
+ throw new P2PXWikiException(P2PXWikiException.COMMUNICATION, "Cannot execute remote call because of an unhandled IO exception", ex);
}
}
Modified: xwiki-clients/p2pxwiki/src/main/java/com/xpn/p2pxwiki/communication/rpc/RpcHandlerStub.java
===================================================================
--- xwiki-clients/p2pxwiki/src/main/java/com/xpn/p2pxwiki/communication/rpc/RpcHandlerStub.java 2006-08-23 09:08:38 UTC (rev 1196)
+++ xwiki-clients/p2pxwiki/src/main/java/com/xpn/p2pxwiki/communication/rpc/RpcHandlerStub.java 2006-08-23 09:32:19 UTC (rev 1197)
@@ -35,10 +35,10 @@
return connection.execute(function, args);
}
catch(XmlRpcException ex){
- throw new P2PXWikiException(P2PXWikiException.ERROR_P2PXWIKI_REMOTECALL, "Cannot execute remote call because of an unhandled RPC exception", ex);
+ throw new P2PXWikiException(P2PXWikiException.REMOTE_CALL, "Cannot execute remote call because of an unhandled RPC exception", ex);
}
catch(IOException ex){
- throw new P2PXWikiException(P2PXWikiException.ERROR_P2PXWIKI_COMMUNICATION, "Cannot execute remote call because of an unhandled IO exception", ex);
+ throw new P2PXWikiException(P2PXWikiException.COMMUNICATION, "Cannot execute remote call because of an unhandled IO exception", ex);
}
}
Modified: xwiki-clients/p2pxwiki/src/main/java/com/xpn/p2pxwiki/services/P2PServicesPlugin.java
===================================================================
--- xwiki-clients/p2pxwiki/src/main/java/com/xpn/p2pxwiki/services/P2PServicesPlugin.java 2006-08-23 09:08:38 UTC (rev 1196)
+++ xwiki-clients/p2pxwiki/src/main/java/com/xpn/p2pxwiki/services/P2PServicesPlugin.java 2006-08-23 09:32:19 UTC (rev 1197)
@@ -8,8 +8,11 @@
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
+import com.xpn.p2pxwiki.P2PXWikiException;
+import com.xpn.p2pxwiki.communication.Network;
import com.xpn.p2pxwiki.services.file.FileP2PServer;
import com.xpn.p2pxwiki.services.file.FileWikiGroupServices;
+import com.xpn.p2pxwiki.utils.P2PUtil;
import com.xpn.xwiki.XWiki;
import com.xpn.xwiki.XWikiContext;
import com.xpn.xwiki.api.Api;
@@ -70,35 +73,25 @@
log.debug("Configuring");
log.debug("Configuring p2pserver services");
p2pserverServicesType = xwiki.Param(SERVER_TYPE, DEFAULT_SERVER_TYPE);
- // TODO: Remove duplication obtained by copy-paste
// TODO: Retry with default server services type on failure, if this isn't already the default
- try {
- p2pserverServices = (P2PServer) Class.forName(p2pserverServicesType).getMethod("getInstance", (Class[]) null).invoke(null, (Object[]) null);
- } catch (ClassNotFoundException ex) {
- log.error("Cannot load p2pserver services type", ex);
- } catch (NoSuchMethodException ex) {
- log.error("Not a p2pserver services type", ex);
- } catch (InvocationTargetException ex) {
- log.error("Something fishy going on...", ex);
- } catch (IllegalAccessException ex) {
- log.error("Something fishy going on...", ex);
+ try{
+ p2pserverServices = (P2PServer)P2PUtil.getSingletonInstance(p2pserverServicesType);
}
+ catch(P2PXWikiException ex){
+ log.error("Cannot load p2pserver services", ex);
+ // we continue and hope for the best
+ }
log.debug("Obtained p2pserver services: " + p2pserverServices.getClass().getCanonicalName());
log.debug("Configuring group services");
groupServicesType = xwiki.Param(GROUP_TYPE, DEFAULT_GROUP_TYPE);
- // TODO: Remove duplication obtained by copy-paste
// TODO: Retry with default group services type on failure, if this isn't already the default
- try {
- groupServices = (WikiGroupServices) Class.forName(groupServicesType).getMethod("getInstance", (Class[]) null).invoke(null, (Object[]) null);
- } catch (ClassNotFoundException ex) {
+ try{
+ groupServices = (WikiGroupServices)P2PUtil.getSingletonInstance(groupServicesType);
+ }
+ catch(P2PXWikiException ex){
log.error("Cannot load group services", ex);
- } catch (NoSuchMethodException ex) {
- log.error("Not a group services type", ex);
- } catch (InvocationTargetException ex) {
- log.error("Something fishy going on...", ex);
- } catch (IllegalAccessException ex) {
- log.error("Something fishy going on...", ex);
+ // we continue and hope for the best
}
log.debug("Obtained group services: " + groupServices.getClass().getCanonicalName());
}
Modified: xwiki-clients/p2pxwiki/src/main/java/com/xpn/p2pxwiki/services/file/FileGroup.java
===================================================================
--- xwiki-clients/p2pxwiki/src/main/java/com/xpn/p2pxwiki/services/file/FileGroup.java 2006-08-23 09:08:38 UTC (rev 1196)
+++ xwiki-clients/p2pxwiki/src/main/java/com/xpn/p2pxwiki/services/file/FileGroup.java 2006-08-23 09:32:19 UTC (rev 1197)
@@ -51,7 +51,7 @@
}
log.debug("Group master: " + this.master);
} catch (FileNotFoundException ex) {
- throw new P2PXWikiException(P2PXWikiException.ERROR_P2PXWIKI_NO_SUCH_GROUP, "Invalid group " + name);
+ throw new P2PXWikiException(P2PXWikiException.NO_SUCH_GROUP, "Invalid group " + name);
} catch (ParserConfigurationException ex) {
throw new P2PXWikiException(XWikiException.ERROR_DOC_XML_PARSING, "Error while parsing group definition file for \"" + name + "\"");
} catch (IOException ex) {
Modified: xwiki-clients/p2pxwiki/src/main/java/com/xpn/p2pxwiki/services/file/FileP2PServer.java
===================================================================
--- xwiki-clients/p2pxwiki/src/main/java/com/xpn/p2pxwiki/services/file/FileP2PServer.java 2006-08-23 09:08:38 UTC (rev 1196)
+++ xwiki-clients/p2pxwiki/src/main/java/com/xpn/p2pxwiki/services/file/FileP2PServer.java 2006-08-23 09:32:19 UTC (rev 1197)
@@ -81,7 +81,7 @@
if(group.isMember(this.name)){
return false;
} else {
- throw new P2PXWikiException(P2PXWikiException.ERROR_P2PXWIKI_ACCESSDENIED, "Groups cannot be altered under the current policy");
+ throw new P2PXWikiException(P2PXWikiException.ACCESS_DENIED, "Groups cannot be altered under the current policy");
}
}
@@ -96,7 +96,7 @@
if(!group.isMember(this.name)){
return false;
} else {
- throw new P2PXWikiException(P2PXWikiException.ERROR_P2PXWIKI_ACCESSDENIED, "Groups cannot be altered under the current policy");
+ throw new P2PXWikiException(P2PXWikiException.ACCESS_DENIED, "Groups cannot be altered under the current policy");
}
}
Modified: xwiki-clients/p2pxwiki/src/main/java/com/xpn/p2pxwiki/services/file/FileWikiGroupServices.java
===================================================================
--- xwiki-clients/p2pxwiki/src/main/java/com/xpn/p2pxwiki/services/file/FileWikiGroupServices.java 2006-08-23 09:08:38 UTC (rev 1196)
+++ xwiki-clients/p2pxwiki/src/main/java/com/xpn/p2pxwiki/services/file/FileWikiGroupServices.java 2006-08-23 09:32:19 UTC (rev 1197)
@@ -59,7 +59,7 @@
public WikiGroup createWikiGroup(String groupName) throws P2PXWikiException {
log.debug("createGroup");
- throw new P2PXWikiException(P2PXWikiException.ERROR_P2PXWIKI_GROUP_NOTALLOWED, "No groups can be created using the current policy");
+ throw new P2PXWikiException(P2PXWikiException.GROUP_NOT_ALLOWED, "No groups can be created using the current policy");
}
public String[] getAllGroupsNames() throws P2PXWikiException {
@@ -86,7 +86,7 @@
public WikiGroup getWikiGroup(String groupName) throws P2PXWikiException {
log.debug("getGroup: " + groupName);
if (!this.groups.contains(groupName)) {
- throw new P2PXWikiException(P2PXWikiException.ERROR_P2PXWIKI_NO_SUCH_GROUP, "Invalid group " + groupName);
+ throw new P2PXWikiException(P2PXWikiException.NO_SUCH_GROUP, "Invalid group " + groupName);
}
if (cache.containsKey(groupName)) {
log.debug("Found group " + groupName + " in cache");
Added: xwiki-clients/p2pxwiki/src/main/java/com/xpn/p2pxwiki/utils/P2PUtil.java
===================================================================
--- xwiki-clients/p2pxwiki/src/main/java/com/xpn/p2pxwiki/utils/P2PUtil.java 2006-08-23 09:08:38 UTC (rev 1196)
+++ xwiki-clients/p2pxwiki/src/main/java/com/xpn/p2pxwiki/utils/P2PUtil.java 2006-08-23 09:32:19 UTC (rev 1197)
@@ -0,0 +1,37 @@
+package com.xpn.p2pxwiki.utils;
+
+import java.lang.reflect.InvocationTargetException;
+
+import com.xpn.p2pxwiki.P2PXWikiException;
+
+public class P2PUtil {
+ public static Object getInstance(String className) throws P2PXWikiException {
+ try{
+ return Class.forName(className).getConstructor((Class[])null).newInstance((Object[])null);
+ } catch(ClassNotFoundException ex){
+ throw new P2PXWikiException(P2PXWikiException.INSTANCE, ex);
+ } catch(NoSuchMethodException ex){
+ throw new P2PXWikiException(P2PXWikiException.INSTANCE, ex);
+ } catch(InvocationTargetException ex){
+ throw new P2PXWikiException(P2PXWikiException.INSTANCE, ex);
+ } catch (InstantiationException ex) {
+ throw new P2PXWikiException(P2PXWikiException.INSTANCE, ex);
+ } catch(IllegalAccessException ex){
+ throw new P2PXWikiException(P2PXWikiException.INSTANCE, ex);
+ }
+ }
+
+ public static Object getSingletonInstance(String className) throws P2PXWikiException {
+ try{
+ return Class.forName(className).getMethod("getInstance", (Class[])null).invoke(null, (Object[])null);
+ } catch(ClassNotFoundException ex){
+ throw new P2PXWikiException(P2PXWikiException.SINGLETON_INSTANCE, ex);
+ } catch(NoSuchMethodException ex){
+ throw new P2PXWikiException(P2PXWikiException.SINGLETON_INSTANCE, ex);
+ } catch(InvocationTargetException ex){
+ throw new P2PXWikiException(P2PXWikiException.SINGLETON_INSTANCE, ex);
+ } catch(IllegalAccessException ex){
+ throw new P2PXWikiException(P2PXWikiException.SINGLETON_INSTANCE, ex);
+ }
+ }
+}
Modified: xwiki-clients/p2pxwiki/src/test/java/com/xpn/p2pxwiki/communication/AbstractLocalTest.java
===================================================================
--- xwiki-clients/p2pxwiki/src/test/java/com/xpn/p2pxwiki/communication/AbstractLocalTest.java 2006-08-23 09:08:38 UTC (rev 1196)
+++ xwiki-clients/p2pxwiki/src/test/java/com/xpn/p2pxwiki/communication/AbstractLocalTest.java 2006-08-23 09:32:19 UTC (rev 1197)
@@ -1,11 +1,7 @@
package com.xpn.p2pxwiki.communication;
-
import junit.framework.TestCase;
-import com.xpn.p2pxwiki.communication.jxta.JxtaConnectionFactory;
-import com.xpn.p2pxwiki.communication.jxta.JxtaHandlerManager;
-import com.xpn.p2pxwiki.communication.jxta.JxtaNetwork;
import com.xpn.p2pxwiki.mocks.HelloHandler;
import com.xpn.xwiki.XWiki;
import com.xpn.xwiki.XWikiConfig;
@@ -22,6 +18,7 @@
private XWiki xwiki;
private ConnectorPlugin plugin;
private Handler handler1;
+ private HandlerStub stub;
public AbstractLocalTest(String arg0) {
super(arg0);
@@ -51,20 +48,18 @@
plugin = new ConnectorPlugin(ConnectorPlugin.PEER_NAME, ConnectorPlugin.class.getName(), xcontext);
handler1 = new HelloHandler();
-// Handler() {
-// public String getName() { return "handler1"; }
-// public String hello(String world) { return "hello "+world; }
-// };
}
-
public void tearDown() throws Exception {
+ plugin.closeConnection(stub);
+ plugin.removeHandler(handler1);
}
public void test() throws XWikiException {
assertTrue(plugin.addHandler(handler1));
- HandlerStub stub = plugin.getConnection(remotePeer);
+ stub = plugin.getConnection(remotePeer);
assertEquals(remotePeer, stub.getPeerName());
+ assertEquals("handler1", stub.execute("handler1.getName", new Object[] {}));
assertEquals("hello world", stub.execute("handler1.hello", new Object[] {"world"}));
assertTrue(plugin.removeHandler(handler1));
}
Modified: xwiki-clients/p2pxwiki/src/test/java/com/xpn/p2pxwiki/communication/RpcLocalTest.java
===================================================================
--- xwiki-clients/p2pxwiki/src/test/java/com/xpn/p2pxwiki/communication/RpcLocalTest.java 2006-08-23 09:08:38 UTC (rev 1196)
+++ xwiki-clients/p2pxwiki/src/test/java/com/xpn/p2pxwiki/communication/RpcLocalTest.java 2006-08-23 09:32:19 UTC (rev 1197)
@@ -1,5 +1,7 @@
package com.xpn.p2pxwiki.communication;
+import junit.textui.TestRunner;
+
import com.xpn.p2pxwiki.communication.rpc.RpcConnectionFactory;
import com.xpn.p2pxwiki.communication.rpc.RpcHandlerManager;
import com.xpn.p2pxwiki.communication.rpc.RpcNetwork;
@@ -26,4 +28,7 @@
return RpcNetwork.class.getName();
}
+ public static void main(String[] args) {
+ TestRunner.run(RpcLocalTest.class);
+ }
}
More information about the Xwiki-notifications
mailing list