[jboss-svn-commits] JBL Code SVN: r32148 - in labs/jbossrules/branches/drools_server_camel_lucaz_baunax/drools-server: src/main/java/org and 9 other directories.

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Fri Mar 19 11:15:52 EDT 2010


Author: lucazamador
Date: 2010-03-19 11:15:50 -0400 (Fri, 19 Mar 2010)
New Revision: 32148

Added:
   labs/jbossrules/branches/drools_server_camel_lucaz_baunax/drools-server/src/main/java/org/apache/
   labs/jbossrules/branches/drools_server_camel_lucaz_baunax/drools-server/src/main/java/org/apache/camel/
   labs/jbossrules/branches/drools_server_camel_lucaz_baunax/drools-server/src/main/java/org/apache/camel/jboss/
   labs/jbossrules/branches/drools_server_camel_lucaz_baunax/drools-server/src/main/java/org/apache/camel/jboss/JBossPackageScanClassResolver.java
   labs/jbossrules/branches/drools_server_camel_lucaz_baunax/drools-server/src/test/resources/cxf-test.xml
Modified:
   labs/jbossrules/branches/drools_server_camel_lucaz_baunax/drools-server/pom.xml
   labs/jbossrules/branches/drools_server_camel_lucaz_baunax/drools-server/src/main/java/org/drools/server/KnowledgeService.java
   labs/jbossrules/branches/drools_server_camel_lucaz_baunax/drools-server/src/main/java/org/drools/server/service/KnowledgeServiceRest.java
   labs/jbossrules/branches/drools_server_camel_lucaz_baunax/drools-server/src/main/java/org/drools/server/service/KnowledgeServiceSoapImpl.java
   labs/jbossrules/branches/drools_server_camel_lucaz_baunax/drools-server/src/main/webapp/WEB-INF/cxf.xml
   labs/jbossrules/branches/drools_server_camel_lucaz_baunax/drools-server/src/main/webapp/index.jsp
   labs/jbossrules/branches/drools_server_camel_lucaz_baunax/drools-server/src/test/java/org/drools/server/service/KnowledgeServiceRestTest.java
   labs/jbossrules/branches/drools_server_camel_lucaz_baunax/drools-server/src/test/java/org/drools/server/service/KnowledgeServiceSoapTest.java
Log:
Added JBossPackageScanClassResolver. Fixed integration JBoss AS 5

Modified: labs/jbossrules/branches/drools_server_camel_lucaz_baunax/drools-server/pom.xml
===================================================================
--- labs/jbossrules/branches/drools_server_camel_lucaz_baunax/drools-server/pom.xml	2010-03-19 09:56:23 UTC (rev 32147)
+++ labs/jbossrules/branches/drools_server_camel_lucaz_baunax/drools-server/pom.xml	2010-03-19 15:15:50 UTC (rev 32148)
@@ -14,41 +14,101 @@
    <build>
       <finalName>drools-server</finalName>
    </build>
-   <repositories>
-   </repositories>
+   
    <dependencies>
-   	  <dependency>
-      	<groupId>org.drools</groupId>
-      	<artifactId>drools-camel</artifactId>
-      	<version>${project.version}</version>
-      </dependency>
-      <dependency>
-      	<groupId>org.drools</groupId>
-      	<artifactId>drools-vsm</artifactId>
-      	<version>${project.version}</version>
-      </dependency>
-      <dependency>
-         <groupId>org.drools</groupId>
-         <artifactId>drools-compiler</artifactId>
-      </dependency>
-      <!-- CXF deps -->
-	  <dependency>
+	<dependency>
+		<groupId>org.drools</groupId>
+		<artifactId>drools-camel</artifactId>
+		<version>${project.version}</version>
+		<exclusions>
+			<exclusion>
+				<groupId>xml-apis</groupId>
+				<artifactId>xml-apis</artifactId>
+			</exclusion>
+		</exclusions>
+	</dependency>
+	<dependency>
+		<groupId>org.drools</groupId>
+		<artifactId>drools-vsm</artifactId>
+		<version>${project.version}</version>
+		<exclusions>
+			<exclusion>
+				<groupId>org.drools</groupId>
+				<artifactId>drools-process-task</artifactId>
+			</exclusion>
+			<exclusion>
+				<groupId>xml-apis</groupId>
+				<artifactId>xml-apis</artifactId>
+			</exclusion>
+		</exclusions>
+	</dependency>
+	<dependency>
+		<groupId>org.drools</groupId>
+		<artifactId>drools-compiler</artifactId>
+	</dependency>
+    <!-- CXF deps -->
+	<dependency>
 		<groupId>org.apache.cxf</groupId>
 		<artifactId>cxf-rt-frontend-jaxws</artifactId>
 		<version>2.2.6</version>
-	  </dependency>
-	  <dependency>
+	</dependency>
+	<dependency>
 		<groupId>org.apache.cxf</groupId>
 		<artifactId>cxf-bundle-jaxrs</artifactId>
 		<version>2.2.6</version>
-  	  </dependency>
-      
-	  <dependency>
-	  	<groupId>commons-httpclient</groupId>
-	  	<artifactId>commons-httpclient</artifactId>
-	  	<version>3.1</version>
-	  	<scope>test</scope>
-	  </dependency>
+		<exclusions>
+			<exclusion>
+				<groupId>xerces</groupId>
+				<artifactId>xercesImpl</artifactId>
+			</exclusion>
+			<exclusion>
+				<groupId>xerces</groupId>
+				<artifactId>xmlParserAPIs</artifactId>
+			</exclusion>
+			<exclusion>
+				<groupId>xml-apis</groupId>
+				<artifactId>xml-apis</artifactId>
+			</exclusion>
+			<exclusion>
+				<groupId>org.apache.geronimo.specs</groupId>
+				<artifactId>geronimo-servlet_2.5_spec</artifactId>
+			</exclusion>
+		</exclusions>
+	</dependency>
+
+	<dependency>
+		<groupId>commons-httpclient</groupId>
+		<artifactId>commons-httpclient</artifactId>
+		<version>3.1</version>
+		<scope>test</scope>
+	</dependency>
+	
+	<dependency>
+		<groupId>org.springframework</groupId>
+		<artifactId>spring-test</artifactId>
+		<version>2.5.6</version>
+		<scope>test</scope>  
+	</dependency>  
+	
+	<dependency>
+		<groupId>junit</groupId>
+		<artifactId>junit</artifactId>
+		<version>4.4</version>
+		<scope>test</scope>  
+	</dependency> 
+
+	<dependency>
+		<groupId>org.jboss</groupId>
+		<artifactId>jboss-vfs</artifactId>
+		<version>2.1.3.GA</version>
+		<scope>provided</scope>
+	</dependency>
+
+	<!-- web app deps -->
+	<dependency>
+		<groupId>javax.servlet</groupId>
+		<artifactId>servlet-api</artifactId>
+	</dependency>
 	  
    </dependencies>
    

Added: labs/jbossrules/branches/drools_server_camel_lucaz_baunax/drools-server/src/main/java/org/apache/camel/jboss/JBossPackageScanClassResolver.java
===================================================================
--- labs/jbossrules/branches/drools_server_camel_lucaz_baunax/drools-server/src/main/java/org/apache/camel/jboss/JBossPackageScanClassResolver.java	                        (rev 0)
+++ labs/jbossrules/branches/drools_server_camel_lucaz_baunax/drools-server/src/main/java/org/apache/camel/jboss/JBossPackageScanClassResolver.java	2010-03-19 15:15:50 UTC (rev 32148)
@@ -0,0 +1,81 @@
+package org.apache.camel.jboss;
+
+import java.io.IOException;
+import java.net.URL;
+import java.util.Enumeration;
+import java.util.Set;
+
+import org.apache.camel.impl.DefaultPackageScanClassResolver;
+import org.apache.camel.spi.PackageScanFilter;
+import org.jboss.virtual.VFS;
+import org.jboss.virtual.VirtualFile;
+import org.jboss.virtual.VisitorAttributes;
+import org.jboss.virtual.plugins.vfs.helpers.AbstractVirtualFileVisitor;
+
+/**
+ * JBoss specific package scan classloader to be used when Camel is running
+ * inside JBoss Application Server.
+ */
+public class JBossPackageScanClassResolver extends DefaultPackageScanClassResolver {
+
+    @Override
+    protected void find(PackageScanFilter test, String packageName, ClassLoader loader, Set<Class<?>> classes) {
+        if (log.isTraceEnabled()) {
+            log.trace("Searching for: " + test + " in package: " + packageName
+                    + " using classloader: " + loader.getClass().getName());
+        }
+
+        Enumeration<URL> urls;
+        try {
+            urls = getResources(loader, packageName);
+            if (!urls.hasMoreElements()) {
+                log.trace("No URLs returned by classloader");
+            }
+        }
+        catch (IOException ioe) {
+            log.warn("Could not read package: " + packageName, ioe);
+            return;
+        }
+
+        while (urls.hasMoreElements()) {
+            URL url = null;
+            try {
+                url = urls.nextElement();
+                if (log.isTraceEnabled()) {
+                    log.trace("URL from classloader: " + url);
+                }
+                VirtualFile root = VFS.getRoot(url);
+                root.visit(new MatchingClassVisitor(test, classes));
+            }
+            catch (IOException ioe) {
+                log.warn("Could not read entries in url: " + url, ioe);
+            }
+        }
+    }
+
+    private class MatchingClassVisitor extends AbstractVirtualFileVisitor {
+        private PackageScanFilter filter;
+        private Set<Class<?>> classes;
+
+        private MatchingClassVisitor(PackageScanFilter filter, Set<Class<?>> classes) {
+            super(VisitorAttributes.RECURSE_LEAVES_ONLY);
+            this.filter = filter;
+            this.classes = classes;
+        }
+
+        public void visit(VirtualFile file) {
+            if (file.getName().endsWith(".class")) {
+                String fqn = file.getPathName();
+                String qn;
+                if (fqn.indexOf("jar/") != -1) {
+                    qn = fqn.substring(fqn.indexOf("jar/") + 4);
+                } else {
+                    qn = fqn.substring(fqn.indexOf("/") + 1);
+                }
+
+                addIfMatching(filter, qn, classes);
+            }
+        }
+    }
+
+}


Property changes on: labs/jbossrules/branches/drools_server_camel_lucaz_baunax/drools-server/src/main/java/org/apache/camel/jboss/JBossPackageScanClassResolver.java
___________________________________________________________________
Name: svn:eol-style
   + native

Modified: labs/jbossrules/branches/drools_server_camel_lucaz_baunax/drools-server/src/main/java/org/drools/server/KnowledgeService.java
===================================================================
--- labs/jbossrules/branches/drools_server_camel_lucaz_baunax/drools-server/src/main/java/org/drools/server/KnowledgeService.java	2010-03-19 09:56:23 UTC (rev 32147)
+++ labs/jbossrules/branches/drools_server_camel_lucaz_baunax/drools-server/src/main/java/org/drools/server/KnowledgeService.java	2010-03-19 15:15:50 UTC (rev 32148)
@@ -6,9 +6,11 @@
 import java.util.ArrayList;
 import java.util.List;
 import java.util.Map;
+import java.util.Properties;
 import java.util.concurrent.ConcurrentHashMap;
 
 import javax.naming.Context;
+import javax.naming.InitialContext;
 import javax.xml.bind.JAXBContext;
 import javax.xml.parsers.DocumentBuilderFactory;
 
@@ -16,7 +18,7 @@
 import org.apache.camel.ProducerTemplate;
 import org.apache.camel.builder.RouteBuilder;
 import org.apache.camel.impl.DefaultCamelContext;
-import org.apache.camel.util.jndi.JndiContext;
+import org.apache.camel.spi.PackageScanClassResolver;
 import org.drools.CheckedDroolsException;
 import org.drools.KnowledgeBase;
 import org.drools.builder.KnowledgeBuilder;
@@ -61,15 +63,12 @@
 	private KnowledgeServerProfile profiles;
 	private ServiceManager serviceManager;
 	private ProducerTemplate template;
+	private PackageScanClassResolver packageScanClassResolver;
 
 	private KnowledgeService() {
 		serviceManager = new ServiceManagerLocalClient();
 		try {
-			Context context = new JndiContext();
-			context.bind("sm", serviceManager);
-			camelContext = new DefaultCamelContext(context);
-			camelContext.start();
-			template = camelContext.createProducerTemplate();
+			startCamelContext(null);
 			readKnowledgeContextProfiles();
 		} catch (Exception e) {
 			LOG.error(e.getMessage());
@@ -221,5 +220,28 @@
 		}
 		return d.getDocumentElement().getAttribute("lookup");
 	}
+	
+	private void startCamelContext(PackageScanClassResolver jbossResolver) throws Exception {
+		Properties env = new Properties();
+		env.setProperty("java.naming.factory.initial", "org.apache.camel.util.jndi.CamelInitialContextFactory");
+		Context context = new InitialContext(env);
+		context.bind("sm", serviceManager);
+		camelContext = new DefaultCamelContext(context);
+		if (jbossResolver!=null) {
+			camelContext.setPackageScanClassResolver(jbossResolver);
+		}
+		camelContext.disableJMX();
+		camelContext.start();
+		template = camelContext.createProducerTemplate();
+	}
 
+	public void setPackageScanClassResolver(PackageScanClassResolver classResolver) throws Exception {
+		this.packageScanClassResolver = classResolver;
+		startCamelContext(this.packageScanClassResolver);
+	}
+
+	public PackageScanClassResolver getsetPackageScanClassResolver() {
+		return packageScanClassResolver;
+	}
+
 }

Modified: labs/jbossrules/branches/drools_server_camel_lucaz_baunax/drools-server/src/main/java/org/drools/server/service/KnowledgeServiceRest.java
===================================================================
--- labs/jbossrules/branches/drools_server_camel_lucaz_baunax/drools-server/src/main/java/org/drools/server/service/KnowledgeServiceRest.java	2010-03-19 09:56:23 UTC (rev 32147)
+++ labs/jbossrules/branches/drools_server_camel_lucaz_baunax/drools-server/src/main/java/org/drools/server/service/KnowledgeServiceRest.java	2010-03-19 15:15:50 UTC (rev 32148)
@@ -10,7 +10,6 @@
 import javax.ws.rs.core.Response.Status;
 
 import org.drools.CheckedDroolsException;
-import org.drools.RuntimeDroolsException;
 import org.drools.server.KnowledgeService;
 import org.drools.server.profile.KnowledgeContextProfile;
 import org.slf4j.Logger;
@@ -18,40 +17,32 @@
 
 /**
  * RestWS interface to execute commands
- * @author lucas
+ * @author Lucas Amador
  *
  */
 @Path("/rest")
 public class KnowledgeServiceRest {
 	
 	private static Logger logger = LoggerFactory.getLogger(KnowledgeServiceRest.class);
-	private static KnowledgeService service;
+	private KnowledgeService service;
 	
-	static {
-		try {
-			service = KnowledgeService.getInstance();
-		} catch (Exception e) {
-			throw new RuntimeDroolsException(e);
-		}
-	}
-	
 	@POST()
 	@Path("/execute")
 	public Response execute(@FormParam("command") String command) {
 		if (command==null || command.length()==0) {
-			logger.error("Invalid or null command");			
+			logger.error("Invalid or null command " + command);			
 			return Response.status(Status.BAD_REQUEST).build();
 		}
 		String response;
 		try {
-			String profile = service.getLookup(command);
-			KnowledgeContextProfile contextProfile = service.getContextProfile(profile);
+			String profile = getService().getLookup(command);
+			KnowledgeContextProfile contextProfile = getService().getContextProfile(profile);
 			if (contextProfile==null) {
 				logger.error("Context profile don't exists");
 				return Response.status(Status.FORBIDDEN).build();
 			}
-			service.checkContextStatus(contextProfile);
-			response = service.executeCommand(contextProfile, command);
+			getService().checkContextStatus(contextProfile);
+			response = getService().executeCommand(contextProfile, command);
 		} catch (CheckedDroolsException e) {
 			logger.error(e.getMessage());
 			return Response.status(Status.BAD_REQUEST).build();
@@ -65,5 +56,13 @@
 		ResponseBuilder builder = Response.ok(response, "application/xml");
         return builder.build();
 	}
+
+	public void setService(KnowledgeService service) {
+		this.service = service;
+	}
+
+	public KnowledgeService getService() {
+		return service;
+	}
 	
 }

Modified: labs/jbossrules/branches/drools_server_camel_lucaz_baunax/drools-server/src/main/java/org/drools/server/service/KnowledgeServiceSoapImpl.java
===================================================================
--- labs/jbossrules/branches/drools_server_camel_lucaz_baunax/drools-server/src/main/java/org/drools/server/service/KnowledgeServiceSoapImpl.java	2010-03-19 09:56:23 UTC (rev 32147)
+++ labs/jbossrules/branches/drools_server_camel_lucaz_baunax/drools-server/src/main/java/org/drools/server/service/KnowledgeServiceSoapImpl.java	2010-03-19 15:15:50 UTC (rev 32148)
@@ -3,7 +3,6 @@
 import javax.jws.WebService;
 
 import org.drools.CheckedDroolsException;
-import org.drools.RuntimeDroolsException;
 import org.drools.server.KnowledgeService;
 import org.drools.server.profile.KnowledgeContextProfile;
 
@@ -12,31 +11,31 @@
 
 	private KnowledgeService service;
 	
-	public KnowledgeServiceSoapImpl() {
-		try {
-			service = KnowledgeService.getInstance();
-		} catch (Exception e) {
-			throw new RuntimeDroolsException(e);
-		}
-	}
-	
 	public String execute(String command) throws CheckedDroolsException {
 		if (command==null || command.length()==0) {
 			throw new CheckedDroolsException("Invalid or null command");
 		}
 		String response = null;
 		try {
-			String profile = service.getLookup(command);
-			KnowledgeContextProfile contextProfile = service.getContextProfile(profile);
+			String profile = getService().getLookup(command);
+			KnowledgeContextProfile contextProfile = getService().getContextProfile(profile);
 			if (contextProfile==null) {
 				throw new CheckedDroolsException("Context profile " + profile + " don't exists");
 			}
-			service.checkContextStatus(contextProfile);
-			response = service.executeCommand(contextProfile, command);
+			getService().checkContextStatus(contextProfile);
+			response = getService().executeCommand(contextProfile, command);
 		} catch (Exception e) {
 			throw new CheckedDroolsException(e.getMessage());
 		}
         return response;
 	}
 
+	public void setService(KnowledgeService service) {
+		this.service = service;
+	}
+
+	public KnowledgeService getService() {
+		return service;
+	}
+
 }
\ No newline at end of file

Modified: labs/jbossrules/branches/drools_server_camel_lucaz_baunax/drools-server/src/main/webapp/WEB-INF/cxf.xml
===================================================================
--- labs/jbossrules/branches/drools_server_camel_lucaz_baunax/drools-server/src/main/webapp/WEB-INF/cxf.xml	2010-03-19 09:56:23 UTC (rev 32147)
+++ labs/jbossrules/branches/drools_server_camel_lucaz_baunax/drools-server/src/main/webapp/WEB-INF/cxf.xml	2010-03-19 15:15:50 UTC (rev 32148)
@@ -25,7 +25,11 @@
     <import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
     
     <!-- The service bean -->
-    <bean id="knowledgeServiceSoapImpl" class="org.drools.server.service.KnowledgeServiceSoapImpl"/>
+    <bean id="knowledgeServiceSoapImpl" class="org.drools.server.service.KnowledgeServiceSoapImpl">
+    	<property name="service">
+			<ref local="knowledgeService" />
+		</property>
+    </bean>
 
     <!-- Service endpoint -->
     <jaxws:endpoint id="knowledgeServiceSoap"
@@ -40,6 +44,18 @@
         </jaxrs:serviceBeans>
     </jaxrs:server>
 
-    <bean id="knowledgeServiceRestImpl" class="org.drools.server.service.KnowledgeServiceRest" />
-    
+    <bean id="knowledgeServiceRestImpl" class="org.drools.server.service.KnowledgeServiceRest" >
+    	<property name="service">
+			<ref local="knowledgeService" />
+		</property>
+    </bean>
+
+    <bean id="knowledgeService" class="org.drools.server.KnowledgeService" >
+    	<property name="packageScanClassResolver">
+			<ref local="jbossResolver" />
+		</property>
+    </bean>
+
+    <bean id="jbossResolver" class="org.apache.camel.jboss.JBossPackageScanClassResolver" />
+
 </beans>
\ No newline at end of file

Modified: labs/jbossrules/branches/drools_server_camel_lucaz_baunax/drools-server/src/main/webapp/index.jsp
===================================================================
--- labs/jbossrules/branches/drools_server_camel_lucaz_baunax/drools-server/src/main/webapp/index.jsp	2010-03-19 09:56:23 UTC (rev 32147)
+++ labs/jbossrules/branches/drools_server_camel_lucaz_baunax/drools-server/src/main/webapp/index.jsp	2010-03-19 15:15:50 UTC (rev 32148)
@@ -1,4 +1,3 @@
-<%@ page import="org.drools.server.*" %>
 <%@ page contentType="text/html; charset=UTF-8" %>
 <html>
 	<head>
@@ -6,40 +5,5 @@
 	</head>
 	<body>
 		<h1>Execution server is running</h1>
-		This server allows you to execute rules/knowledge bases remotely using a RESTful interface. One service per RuleAgent configuration (you can have as many as needed).
-		<h2>Stateless services</h2>
-		<h3>URL:</h3>http://{server address, port etc}<%=request.getRequestURI() + "knowledgebase/{configurationName}" %><br/>
-		a HTTP POST to this URL will perform a stateless execution of the knowledgebase/rules.
-		<p/>
-		The {configurationName} is the name of a configured rule agent properties file (not incliding the .properties extension).
-		This configuration then declares what packages are used, where the repository is etc (this can work with both Guvnor, but also DRL files, etc).
-		This properties file must be in the classpath for this war - in the WEB-INF/classes directory.
-		<h3>Sample request content:</h3>
-		<p>
-		A request may contain queries, globals, inOutFacts, and inFacts. Globals and inOutFacts may be modified 
-		by the rules and returned in the response. Queries provide a mechanism to declare which queries in the 
-		rule base may be run to return selected facts as outFacts. Queries contain the name of the query, the declarations,
-		and arguments.
-		</p>
-		<textarea rows=20 cols=80><%=KnowledgeStatelessServlet.getRequestExample(false) %></textarea>
-
-		<h3>Sample response content:</h3>
-		<textarea rows=20 cols=80><%=KnowledgeStatelessServlet.getResponseExample(false) %></textarea>
-
-		<h3>General instructions</h3>
-		By default XML will be used, if you pass in a Content-Type header of application/json, then it will use JSON instead.
-		JSON is both a more compact and more performant format.
-		HTTP POST is used to access this service.
-		Any libraries the rules use (such as fact pojos) will need to be in the WEB-INF/lib directory.
-		<p/>
-		<h3>Sample JSON request:</h3>
-		<textarea rows=10 cols=80><%=KnowledgeStatelessServlet.getRequestExample(true) %></textarea>
-
-		<h3>Sample JSON response:</h3>
-		<textarea rows=10 cols=80><%=KnowledgeStatelessServlet.getResponseExample(true) %></textarea>
-
-		</h3>
-
-
 	</body>
 </html>

Modified: labs/jbossrules/branches/drools_server_camel_lucaz_baunax/drools-server/src/test/java/org/drools/server/service/KnowledgeServiceRestTest.java
===================================================================
--- labs/jbossrules/branches/drools_server_camel_lucaz_baunax/drools-server/src/test/java/org/drools/server/service/KnowledgeServiceRestTest.java	2010-03-19 09:56:23 UTC (rev 32147)
+++ labs/jbossrules/branches/drools_server_camel_lucaz_baunax/drools-server/src/test/java/org/drools/server/service/KnowledgeServiceRestTest.java	2010-03-19 15:15:50 UTC (rev 32148)
@@ -1,169 +1,99 @@
 package org.drools.server.service;
 
-import java.io.BufferedReader;
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.InputStreamReader;
-import java.io.UnsupportedEncodingException;
-
+import javax.ws.rs.core.Response;
 import javax.ws.rs.core.Response.Status;
 
-import org.apache.commons.httpclient.HttpClient;
-import org.apache.commons.httpclient.NameValuePair;
-import org.apache.commons.httpclient.methods.PostMethod;
-import org.apache.cxf.endpoint.Server;
-import org.apache.cxf.jaxrs.JAXRSServerFactoryBean;
 import org.drools.runtime.ExecutionResults;
 import org.drools.runtime.help.BatchExecutionHelper;
 import org.drools.runtime.rule.impl.FlatQueryResults;
 import org.drools.server.KnowledgeServiceBaseTest;
+import org.drools.server.service.KnowledgeServiceRest;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Qualifier;
+import org.springframework.test.context.ContextConfiguration;
+import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
 
+ at RunWith(SpringJUnit4ClassRunner.class)
+ at ContextConfiguration(locations = { "classpath:cxf-test.xml" })
 public class KnowledgeServiceRestTest extends KnowledgeServiceBaseTest {
 
-	private static final String SERVICE_URL = "http://localhost:9000/drools-server/services/";
+	@Autowired
+	@Qualifier("knowledgeServiceRestImpl")
+	protected KnowledgeServiceRest proxy;
 
-	private HttpClient httpClient;
-	private Server server;
-
-	protected void setUp() throws Exception {
-		JAXRSServerFactoryBean serverFactory = new JAXRSServerFactoryBean();
-		serverFactory.setServiceClass(KnowledgeServiceRest.class);
-		serverFactory.setAddress(SERVICE_URL);
-		serverFactory.setResourceClasses(KnowledgeServiceRest.class);
-		server = serverFactory.create();
-		httpClient = new HttpClient();
-		httpClient.getHostConfiguration().setHost("localhost", 9000, "http");
-	}
-
+	@Test
 	public void testInvalidCommand() throws Exception {
-		PostMethod postMethod = new PostMethod("/drools-server/services/rest/execute");
-		NameValuePair profile = new NameValuePair("profile", "ksession1");
-		NameValuePair command = new NameValuePair("command", "asdsad");
-
-		postMethod.setRequestBody(new NameValuePair[] {profile, command});
-		httpClient.executeMethod(postMethod);
-
-		assertEquals(Status.BAD_REQUEST.getStatusCode(), postMethod.getStatusCode());
-
-		server.stop();
+		Response response = proxy.execute("asdsad");
+		assertEquals(Status.BAD_REQUEST.getStatusCode(), response.getStatus());
 	}
 
+	@Test
 	public void testInvalidProfile() throws Exception {
-		PostMethod postMethod = new PostMethod("/drools-server/services/rest/execute");
-		NameValuePair command = new NameValuePair("command", "xml definition");
-
-		postMethod.setRequestBody(new NameValuePair[] {command});
-		httpClient.executeMethod(postMethod);
-
-		assertEquals(Status.BAD_REQUEST.getStatusCode(), postMethod.getStatusCode());
-
-		server.stop();
+		Response response = proxy.execute("unknow profile");
+		assertEquals(Status.BAD_REQUEST.getStatusCode(), response.getStatus());
 	}
 
+	@Test
 	public void testJaxbInsertCommand() throws Exception {
 		String cmd = getJaxbCommand();
 
-		PostMethod postMethod = new PostMethod("/drools-server/services/rest/execute");
-		NameValuePair command = new NameValuePair("command", cmd);
+		Response response = proxy.execute(cmd);
 
-		postMethod.setRequestBody(new NameValuePair[] {command});
-		httpClient.executeMethod(postMethod);
+		assertEquals(200, response.getStatus());
 
-		assertEquals(200, postMethod.getStatusCode());
+		String XMLoutput = (String) response.getEntity();
 
-		InputStream responseBodyAsStream = postMethod.getResponseBodyAsStream();
-
-		String response = readInputStream(responseBodyAsStream);
-
-		assertTrue(response.indexOf("<ns2:name>santa</ns2:name>") > -1);
-		assertTrue(response.indexOf("<item key=\"lucaz\">\n            <value xsi:type=\"disconnectedFactHandle\"") > -1);
-		assertTrue(response.indexOf("<item key=\"baunax\">\n            <value xsi:type=\"disconnectedFactHandle\"") > -1);
-
-		server.stop();
+		assertTrue(XMLoutput.indexOf("<ns2:name>santa</ns2:name>") > -1);
+		assertTrue(XMLoutput.indexOf("<item key=\"lucaz\">\n            <value xsi:type=\"disconnectedFactHandle\"") > -1);
+		assertTrue(XMLoutput.indexOf("<item key=\"baunax\">\n            <value xsi:type=\"disconnectedFactHandle\"") > -1);
 	}
 
+	@Test
 	public void testXStreamInsertCommand() throws Exception {
 		String cmd = getXStreamCommand();
+		Response response = proxy.execute(cmd);
+		assertEquals(200, response.getStatus());
 
-		PostMethod postMethod = new PostMethod("/drools-server/services/rest/execute");
-		NameValuePair command = new NameValuePair("command", cmd);
+		String XMLoutput = (String) response.getEntity();
 
-		postMethod.setRequestBody(new NameValuePair[] {command});
-		httpClient.executeMethod(postMethod);
+		ExecutionResults result = (ExecutionResults) BatchExecutionHelper.newXStreamMarshaller().fromXML(XMLoutput);
 
-		assertEquals(200, postMethod.getStatusCode());
-
-		InputStream responseBodyAsStream = postMethod.getResponseBodyAsStream();
-
-		String response = readInputStream(responseBodyAsStream);
-
-		ExecutionResults result = (ExecutionResults) BatchExecutionHelper.newXStreamMarshaller().fromXML(response);
-
 		assertNotNull(result.getFactHandle("lucaz"));
 
 		FlatQueryResults personsQuery = (FlatQueryResults) result.getValue("persons");
 		assertEquals(2, personsQuery.size());
-
-		server.stop();
 	}
 
+	@Test
 	public void testBothsessions() throws Exception {
 		String cmd = getJaxbCommand();
 
-		PostMethod postMethod = new PostMethod("/drools-server/services/rest/execute");
-		NameValuePair command = new NameValuePair("command", cmd);
+		Response response = proxy.execute(cmd);
 
-		postMethod.setRequestBody(new NameValuePair[] {command});
-		httpClient.executeMethod(postMethod);
+		assertEquals(200, response.getStatus());
 
-		assertEquals(200, postMethod.getStatusCode());
+		String XMLoutput = (String) response.getEntity();
 
-		InputStream responseBodyAsStream = postMethod.getResponseBodyAsStream();
+		assertTrue(XMLoutput.indexOf("<ns2:name>santa</ns2:name>") > -1);
+		assertTrue(XMLoutput.indexOf("<item key=\"lucaz\">\n            <value xsi:type=\"disconnectedFactHandle\"") > -1);
+		assertTrue(XMLoutput.indexOf("<item key=\"baunax\">\n            <value xsi:type=\"disconnectedFactHandle\"") > -1);
 
-		String response = readInputStream(responseBodyAsStream);
-
-		assertTrue(response.indexOf("<ns2:name>santa</ns2:name>") > -1);
-		assertTrue(response.indexOf("<item key=\"lucaz\">\n            <value xsi:type=\"disconnectedFactHandle\"") > -1);
-		assertTrue(response.indexOf("<item key=\"baunax\">\n            <value xsi:type=\"disconnectedFactHandle\"") > -1);
-
 		cmd = getXStreamCommand();
 
-		postMethod = new PostMethod("/drools-server/services/rest/execute");
-		command = new NameValuePair("command", cmd);
+		response = proxy.execute(cmd);
 
-		postMethod.setRequestBody(new NameValuePair[] {command});
-		httpClient.executeMethod(postMethod);
+		assertEquals(200, response.getStatus());
 
-		assertEquals(200, postMethod.getStatusCode());
+		XMLoutput = (String) response.getEntity();
 
-		responseBodyAsStream = postMethod.getResponseBodyAsStream();
+		ExecutionResults result = (ExecutionResults) BatchExecutionHelper.newXStreamMarshaller().fromXML(XMLoutput);
 
-		response = readInputStream(responseBodyAsStream);
-
-		ExecutionResults result = (ExecutionResults) BatchExecutionHelper.newXStreamMarshaller().fromXML(response);
-
 		assertNotNull(result.getFactHandle("lucaz"));
 
 		FlatQueryResults personsQuery = (FlatQueryResults) result.getValue("persons");
 		assertEquals(2, personsQuery.size());
-
-		server.stop();
-
 	}
 
-	private String readInputStream(InputStream inputStream) throws UnsupportedEncodingException, IOException {
-		StringBuilder sb = new StringBuilder();
-		String line;
-		try {
-			BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
-			while ((line = reader.readLine()) != null) {
-				sb.append(line).append("\n");
-			}
-		}
-		finally {
-			inputStream.close();
-		}
-		return sb.toString();
-	}
-
 }

Modified: labs/jbossrules/branches/drools_server_camel_lucaz_baunax/drools-server/src/test/java/org/drools/server/service/KnowledgeServiceSoapTest.java
===================================================================
--- labs/jbossrules/branches/drools_server_camel_lucaz_baunax/drools-server/src/test/java/org/drools/server/service/KnowledgeServiceSoapTest.java	2010-03-19 09:56:23 UTC (rev 32147)
+++ labs/jbossrules/branches/drools_server_camel_lucaz_baunax/drools-server/src/test/java/org/drools/server/service/KnowledgeServiceSoapTest.java	2010-03-19 15:15:50 UTC (rev 32148)
@@ -1,85 +1,69 @@
 package org.drools.server.service;
 
-import org.apache.cxf.endpoint.Server;
-import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
-import org.apache.cxf.jaxws.JaxWsServerFactoryBean;
 import org.drools.runtime.ExecutionResults;
 import org.drools.runtime.help.BatchExecutionHelper;
 import org.drools.runtime.rule.impl.FlatQueryResults;
 import org.drools.server.KnowledgeServiceBaseTest;
+import org.drools.server.service.KnowledgeServiceSoap;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Qualifier;
+import org.springframework.test.context.ContextConfiguration;
+import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
 
+ at RunWith(SpringJUnit4ClassRunner.class)
+ at ContextConfiguration(locations = { "classpath:cxf-test.xml" })
 public class KnowledgeServiceSoapTest extends KnowledgeServiceBaseTest {
 
-	private static final String SERVICE_URL = "http://localhost:9000/drools-server/services/soap";
-	private Server server;
+	@Autowired
+	@Qualifier("knowledgeServiceSoapImpl")
+	protected KnowledgeServiceSoap proxy;
 
-	@Override
-	protected void setUp() throws Exception {
-		super.setUp();
-		JaxWsServerFactoryBean serverFactory = new JaxWsServerFactoryBean();
-		serverFactory.setServiceClass(KnowledgeServiceSoap.class);
-		serverFactory.setAddress(SERVICE_URL);
-		serverFactory.setServiceBean(new KnowledgeServiceSoapImpl());
-//		serverFactory.getInInterceptors().add(new LoggingInInterceptor());
-//		serverFactory.getOutInterceptors().add(new LoggingOutInterceptor());
-		server = serverFactory.create();
-	}
+	@Test
+	public void testJaxbInsertCommand() throws Exception {
+		String cmd = getJaxbCommand();
 
-	public void testJaxbCommand() throws Exception {
-		JaxWsProxyFactoryBean clientFactory = new JaxWsProxyFactoryBean();
-		clientFactory.setServiceClass(KnowledgeServiceSoap.class);
-		clientFactory.setAddress(SERVICE_URL);
-		KnowledgeServiceSoap client = (KnowledgeServiceSoap) clientFactory.create();
+		String response = proxy.execute(cmd);
 
-		String response = client.execute(getJaxbCommand());
-		
 		assertTrue(response.indexOf("<ns2:name>santa</ns2:name>") > -1);
 		assertTrue(response.indexOf("<item key=\"lucaz\">\n            <value xsi:type=\"disconnectedFactHandle\"") > -1);
 		assertTrue(response.indexOf("<item key=\"baunax\">\n            <value xsi:type=\"disconnectedFactHandle\"") > -1);
-
-		server.stop();
 	}
 
-	public void testXStreamCommand() throws Exception {
-		JaxWsProxyFactoryBean clientFactory = new JaxWsProxyFactoryBean();
-		clientFactory.setServiceClass(KnowledgeServiceSoap.class);
-		clientFactory.setAddress(SERVICE_URL);
-		KnowledgeServiceSoap client = (KnowledgeServiceSoap) clientFactory.create();
+	@Test
+	public void testXStreamInsertCommand() throws Exception {
+		String cmd = getXStreamCommand();
+		String response = proxy.execute(cmd);
 
-		String response = client.execute(getXStreamCommand());
-		
 		ExecutionResults result = (ExecutionResults) BatchExecutionHelper.newXStreamMarshaller().fromXML(response);
 
 		assertNotNull(result.getFactHandle("lucaz"));
 
 		FlatQueryResults personsQuery = (FlatQueryResults) result.getValue("persons");
 		assertEquals(2, personsQuery.size());
-
-		server.stop();
 	}
 
-	public void testBothSessions() throws Exception {
-		JaxWsProxyFactoryBean clientFactory = new JaxWsProxyFactoryBean();
-		clientFactory.setServiceClass(KnowledgeServiceSoap.class);
-		clientFactory.setAddress(SERVICE_URL);
-		KnowledgeServiceSoap client = (KnowledgeServiceSoap) clientFactory.create();
+	@Test
+	public void testBothsessions() throws Exception {
+		String cmd = getJaxbCommand();
 
-		String response = client.execute(getJaxbCommand());
+		String response = proxy.execute(cmd);
 
 		assertTrue(response.indexOf("<ns2:name>santa</ns2:name>") > -1);
 		assertTrue(response.indexOf("<item key=\"lucaz\">\n            <value xsi:type=\"disconnectedFactHandle\"") > -1);
 		assertTrue(response.indexOf("<item key=\"baunax\">\n            <value xsi:type=\"disconnectedFactHandle\"") > -1);
 
-		response = client.execute(getXStreamCommand());
+		cmd = getXStreamCommand();
 
+		response = proxy.execute(cmd);
+
 		ExecutionResults result = (ExecutionResults) BatchExecutionHelper.newXStreamMarshaller().fromXML(response);
 
 		assertNotNull(result.getFactHandle("lucaz"));
 
 		FlatQueryResults personsQuery = (FlatQueryResults) result.getValue("persons");
 		assertEquals(2, personsQuery.size());
-
-		server.stop();
 	}
 
 }

Added: labs/jbossrules/branches/drools_server_camel_lucaz_baunax/drools-server/src/test/resources/cxf-test.xml
===================================================================
--- labs/jbossrules/branches/drools_server_camel_lucaz_baunax/drools-server/src/test/resources/cxf-test.xml	                        (rev 0)
+++ labs/jbossrules/branches/drools_server_camel_lucaz_baunax/drools-server/src/test/resources/cxf-test.xml	2010-03-19 15:15:50 UTC (rev 32148)
@@ -0,0 +1,54 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<beans xmlns="http://www.springframework.org/schema/beans"
+    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+    xmlns:context="http://www.springframework.org/schema/context"
+    xmlns:cxf="http://cxf.apache.org/core"
+    xmlns:jaxws="http://cxf.apache.org/jaxws"
+    xmlns:jaxrs="http://cxf.apache.org/jaxrs"
+    xsi:schemaLocation="http://www.springframework.org/schema/beans
+        http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
+        http://www.springframework.org/schema/context
+        http://www.springframework.org/schema/context/spring-context-2.5.xsd
+        http://cxf.apache.org/core
+        http://cxf.apache.org/schemas/core.xsd
+        http://cxf.apache.org/jaxws
+        http://cxf.apache.org/schemas/jaxws.xsd
+        http://cxf.apache.org/jaxrs
+		http://cxf.apache.org/schemas/jaxrs.xsd"
+    default-autowire="byName">
+
+    <!-- Load CXF modules from cxf.jar -->
+    <import resource="classpath:META-INF/cxf/cxf.xml" />
+    <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
+    <import resource="classpath:META-INF/cxf/cxf-extension-jaxrs-binding.xml" />
+    <import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
+    
+    <!-- The service bean -->
+    <bean id="knowledgeServiceSoapImpl" class="org.drools.server.service.KnowledgeServiceSoapImpl">
+    	<property name="service">
+			<ref local="knowledgeService" />
+		</property>
+    </bean>
+
+    <!-- Service endpoint -->
+    <jaxws:endpoint id="knowledgeServiceSoap"
+            implementorClass="org.drools.server.service.KnowledgeServiceSoapImpl"
+            implementor="#knowledgeServiceSoapImpl"
+            address="/soap">
+    </jaxws:endpoint>
+    
+    <jaxrs:server id="knowledgeServiceRest" address="/">
+        <jaxrs:serviceBeans>
+            <ref bean="knowledgeServiceRestImpl" />
+        </jaxrs:serviceBeans>
+    </jaxrs:server>
+
+    <bean id="knowledgeServiceRestImpl" class="org.drools.server.service.KnowledgeServiceRest" >
+    	<property name="service">
+			<ref local="knowledgeService" />
+		</property>
+    </bean>
+
+    <bean id="knowledgeService" class="org.drools.server.KnowledgeService" />
+    
+</beans>
\ No newline at end of file


Property changes on: labs/jbossrules/branches/drools_server_camel_lucaz_baunax/drools-server/src/test/resources/cxf-test.xml
___________________________________________________________________
Name: svn:eol-style
   + native



More information about the jboss-svn-commits mailing list