[parancoe] push by lucio.be...@gmail.com - Added REST services for accessing the configuration. on 2012-10-29 08:29 GMT

1 view
Skip to first unread message

para...@googlecode.com

unread,
Oct 29, 2012, 4:29:49 AM10/29/12
to parancoe...@googlegroups.com
Revision: 429c27674ba4
Branch: default
Author: lucio.benfante <lucio.b...@gmail.com>
Date: Mon Oct 29 01:24:47 2012
Log: Added REST services for accessing the configuration.
http://code.google.com/p/parancoe/source/detail?r=429c27674ba4

Added:

/plugins/parancoe-plugin-configuration/src/main/java/org/parancoe/plugin/configuration/PropertyCollection.java

/plugins/parancoe-plugin-configuration/src/main/java/org/parancoe/plugin/configuration/bo/ConfigurationService.java

/plugins/parancoe-plugin-configuration/src/test/java/org/parancoe/plugin/configuration/PopulateInitialDataContextListener.java

/plugins/parancoe-plugin-configuration/src/test/java/org/parancoe/plugin/configuration/RestServicesTest.java

/plugins/parancoe-plugin-configuration/src/test/java/org/parancoe/plugin/configuration/SelfServer.java

/plugins/parancoe-plugin-configuration/src/test/java/org/parancoe/plugin/configuration/TestContextListener.java

/plugins/parancoe-plugin-configuration/src/test/resources/org/parancoe/plugin/configuration/rest-test-client.xml

/plugins/parancoe-plugin-configuration/src/test/resources/org/parancoe/plugin/configuration/restful-services.xml
/plugins/parancoe-plugin-configuration/src/test/webapp/WEB-INF/database.xml
/plugins/parancoe-plugin-configuration/src/test/webapp/WEB-INF/web.xml
Modified:
/parancoe-web/src/main/java/org/parancoe/web/ContextListener.java
/plugins/parancoe-plugin-configuration/pom.xml

/plugins/parancoe-plugin-configuration/src/main/java/org/parancoe/plugin/configuration/ConfigurationCollection.java

/plugins/parancoe-plugin-configuration/src/main/java/org/parancoe/plugin/configuration/bo/ConfigurationManager.java

/plugins/parancoe-plugin-configuration/src/main/java/org/parancoe/plugin/configuration/dao/PropertyDao.java

/plugins/parancoe-plugin-configuration/src/main/java/org/parancoe/plugin/configuration/po/Category.java

/plugins/parancoe-plugin-configuration/src/main/java/org/parancoe/plugin/configuration/po/Property.java
/plugins/parancoe-plugin-configuration/src/test/resources/log4j.properties
/plugins/parancoe-plugin-configuration/src/test/resources/spring-test.xml

=======================================
--- /dev/null
+++
/plugins/parancoe-plugin-configuration/src/main/java/org/parancoe/plugin/configuration/PropertyCollection.java
Mon Oct 29 01:24:47 2012
@@ -0,0 +1,49 @@
+/**
+ * Copyright (C) 2006-2012 The Parancoe Team <in...@parancoe.org>
+ *
+ * This file is part of Parancoe Plugin Configuration.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.parancoe.plugin.configuration;
+
+import java.util.List;
+import javax.xml.bind.annotation.XmlRootElement;
+import org.parancoe.plugin.configuration.po.Property;
+
+/**
+ * A class for collecting configuration properties. Usually it's used for
passing sets of properties
+ * in web services.
+ *
+ * @author Lucio Benfante <lu...@benfante.com>
+ */
+@XmlRootElement
+public class PropertyCollection {
+
+ private List<Property> properties;
+
+ public PropertyCollection() {
+ }
+
+ public PropertyCollection(List<Property> properties) {
+ this.properties = properties;
+ }
+
+ public List<Property> getProperties() {
+ return properties;
+ }
+
+ public void setProperties(List<Property> properties) {
+ this.properties = properties;
+ }
+}
=======================================
--- /dev/null
+++
/plugins/parancoe-plugin-configuration/src/main/java/org/parancoe/plugin/configuration/bo/ConfigurationService.java
Mon Oct 29 01:24:47 2012
@@ -0,0 +1,96 @@
+/**
+ * Copyright (C) 2006-2012 The Parancoe Team <in...@parancoe.org>
+ *
+ * This file is part of Parancoe Plugin Configuration.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.parancoe.plugin.configuration.bo;
+
+import java.util.List;
+import javax.ws.rs.Consumes;
+import javax.ws.rs.GET;
+import javax.ws.rs.PUT;
+import javax.ws.rs.Path;
+import javax.ws.rs.PathParam;
+import javax.ws.rs.Produces;
+import javax.ws.rs.core.MediaType;
+import org.parancoe.plugin.configuration.ConfigurationCollection;
+import org.parancoe.plugin.configuration.PropertyCollection;
+import org.parancoe.plugin.configuration.po.Category;
+import org.parancoe.plugin.configuration.po.Property;
+
+/**
+ *
+ * @author Lucio Benfante <lu...@benfante.com>
+ */
+@Path("configuration")
+@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
+public interface ConfigurationService {
+
+ /**
+ * Return a property of a category.
+ *
+ * @param categoryName The category name.
+ * @param propertyName The property name.
+ * @return A property. null if the category or the property are not
found.
+ */
+ @GET @Path("property/{category}/{property}")
+ Property getProperty(@PathParam("category") String categoryName,
@PathParam("property") String propertyName);
+
+
+ /*
+ * Return all properties of a category.
+ *
+ * @param categoryName The category name.
+ * @return A property collection. null if the category is not found.
+ */
+ @GET @Path("category/{category}/properties")
+ public PropertyCollection getProperties(@PathParam("category") String
categoryName);
+
+ /**
+ * Load a property.
+ *
+ * @param id The property id.
+ * @return A property. null if the property doesn't exist.
+ */
+ @GET @Path("property/{id}")
+ Property getProperty(@PathParam("id") Long id);
+
+ /**
+ * Initialize a configuration, creating the elements (categories and
properties) that doesn't
+ * already exists.
+ *
+ * @param value The configuration collection to initialize.
+ */
+ @PUT
+ @Consumes({MediaType.APPLICATION_JSON})
+ void initializeConfiguration(ConfigurationCollection
configurationCollection);
+
+ /**
+ * Load all configuration categories.
+ *
+ * @return The configuration categories.
+ */
+ @GET @Path("categories")
+ ConfigurationCollection getConfiguration();
+
+ /**
+ * Store a property.
+ *
+ * @param property The property to store.
+ */
+ @PUT
+ void store(Property property);
+
+}
=======================================
--- /dev/null
+++
/plugins/parancoe-plugin-configuration/src/test/java/org/parancoe/plugin/configuration/PopulateInitialDataContextListener.java
Mon Oct 29 01:24:47 2012
@@ -0,0 +1,36 @@
+/**
+ * Copyright (C) 2006-2012 The Parancoe Team <in...@parancoe.org>
+ *
+ * This file is part of Parancoe Plugin Configuration.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.parancoe.plugin.configuration;
+
+import org.parancoe.plugin.configuration.po.Category;
+import org.parancoe.plugin.configuration.po.Property;
+
+/**
+ * A context listener that initialize the database of the application (if
not
+ * initialized yet).
+ *
+ * @author Lucio Benfante
+ */
+public class PopulateInitialDataContextListener extends
org.parancoe.web.PopulateInitialDataContextListener {
+
+ public PopulateInitialDataContextListener() {
+ // Add here to the clazzToPopulate collection the entity classes
you need to populate
+ clazzToPopulate.add(Category.class);
+ clazzToPopulate.add(Property.class);
+ }
+}
=======================================
--- /dev/null
+++
/plugins/parancoe-plugin-configuration/src/test/java/org/parancoe/plugin/configuration/RestServicesTest.java
Mon Oct 29 01:24:47 2012
@@ -0,0 +1,136 @@
+/**
+ * Copyright (C) 2006-2012 The Parancoe Team <in...@parancoe.org>
+ *
+ * This file is part of Parancoe Plugin Configuration.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.parancoe.plugin.configuration;
+
+
+import java.util.List;
+import junit.framework.Assert;
+
+import org.junit.AfterClass;
+import org.junit.BeforeClass;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.parancoe.plugin.configuration.bo.ConfigurationService;
+import org.parancoe.plugin.configuration.po.Category;
+import org.parancoe.plugin.configuration.po.Property;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+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;
+
+/**
+ * Test CXF REST services
+ *
+ * @author Arjun Dhar
+ */
+@RunWith(SpringJUnit4ClassRunner.class)
+@ContextConfiguration(locations = {
+ "classpath:org/parancoe/plugin/configuration/rest-test-client.xml"
+})
+public class RestServicesTest {
+
+ private static Logger log =
LoggerFactory.getLogger(RestServicesTest.class);
+ private static SelfServer server;
+
+ @Autowired
+ @Qualifier("testclient")
+ protected ConfigurationService proxy;
+
+ @Autowired
+ @Qualifier("testclientxml")
+ protected ConfigurationService proxyXml;
+
+ @BeforeClass
+ public static void startServer() throws Exception {
+ /* please note, in actuality, for multiple tests you will have
+ to ensure a single version of the server is running only.
+
+ For each test case, it will invoke start and will give an error.
+ This is simplified for Blog consumption here only. */
+ server = new SelfServer();
+ server.start();
+ }
+
+ @AfterClass
+ public static void stopServer() throws Exception {
+ server.stop();
+ }
+
+ @Test
+ public void testGetPropertyByCategoryAndName() throws Exception {
+ Property property =
proxy.getProperty("first_category", "first_property");
+ Assert.assertNotNull(property);
+ Assert.assertEquals("first_property", property.getName());
+ Assert.assertEquals("first_category",
property.getCategory().getName());
+ }
+
+ @Test
+ public void testGetPropertyById() throws Exception {
+ Property startProperty =
proxy.getProperty("first_category", "first_property");
+ Property property = proxy.getProperty(startProperty.getId());
+ Assert.assertNotNull(property);
+ Assert.assertEquals(startProperty.getId(), property.getId());
+ }
+
+ @Test
+ public void testLoadCategories() throws Exception {
+ ConfigurationCollection configuration = proxy.getConfiguration();
+ Assert.assertNotNull(configuration.getCategories());
+ Assert.assertEquals(2, configuration.getCategories().size());
+ }
+
+ @Test
+ public void testGetProperties() throws Exception {
+ PropertyCollection properties =
proxy.getProperties("first_category");
+ Assert.assertNotNull(properties.getProperties());
+ Assert.assertEquals(2, properties.getProperties().size());
+ }
+
+ @Test
+ public void testXmlGetPropertyByCategoryAndName() throws Exception {
+ Property property =
proxyXml.getProperty("first_category", "first_property");
+ Assert.assertNotNull(property);
+ Assert.assertEquals("first_property", property.getName());
+ Assert.assertEquals("first_category",
property.getCategory().getName());
+ }
+
+ @Test
+ public void testXmlGetPropertyById() throws Exception {
+ Property startProperty =
proxyXml.getProperty("first_category", "first_property");
+ Property property = proxyXml.getProperty(startProperty.getId());
+ Assert.assertNotNull(property);
+ Assert.assertEquals(startProperty.getId(), property.getId());
+ }
+
+ @Test
+ public void testXmlLoadCategories() throws Exception {
+ ConfigurationCollection categories = proxyXml.getConfiguration();
+ Assert.assertNotNull(categories.getCategories());
+ Assert.assertEquals(2, categories.getCategories().size());
+ }
+
+ @Test
+ public void testXmlGetProperties() throws Exception {
+ PropertyCollection properties =
proxyXml.getProperties("first_category");
+ Assert.assertNotNull(properties.getProperties());
+ Assert.assertEquals(2, properties.getProperties().size());
+ }
+
+}
=======================================
--- /dev/null
+++
/plugins/parancoe-plugin-configuration/src/test/java/org/parancoe/plugin/configuration/SelfServer.java
Mon Oct 29 01:24:47 2012
@@ -0,0 +1,175 @@
+/**
+ * Copyright (C) 2006-2012 The Parancoe Team <in...@parancoe.org>
+ *
+ * This file is part of Parancoe Plugin Configuration.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.parancoe.plugin.configuration;
+
+import java.io.BufferedReader;
+import java.io.InputStreamReader;
+import java.io.OutputStream;
+import java.net.InetAddress;
+import java.net.ServerSocket;
+import java.net.Socket;
+
+import org.mortbay.jetty.Connector;
+import org.mortbay.jetty.Server;
+import org.mortbay.jetty.bio.SocketConnector;
+import org.mortbay.jetty.webapp.WebAppContext;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * A Self Server to Start & stop
+ *
+ * Code help taken from: * <a
+ *
href="http://ptrthomas.wordpress.com/2009/01/24/how-to-start-and-stop-jetty-revisited/">How
to
+ * start and stop Jetty – revisited</a>
+ *
+ *
+ * <a
+ *
href="http://www.codeproject.com/Articles/128145/Run-Jetty-Web-Server-Within-Your-Application">Run
+ * Jetty Web Server Within Your Application</a>
+ *
+ * @author Arjun Dhar
+ * @author Lucio Benfante <lucio.b...@gmail.com>
+ */
+public class SelfServer {
+
+ private static Logger log = LoggerFactory.getLogger(SelfServer.class);
+ private Server server;
+ private int port = 9091;
+ private int stopPort = 9092;
+ private String contextPath = "/testContext";
+ private String webPath = "src/test/webapp/WEB-INF";
+ private String host = "127.0.0.1";
+ private WebAppContext context;
+
+ public SelfServer() {
+ }
+
+ public SelfServer(String host, int port, String contextPath, String
webPath) {
+ this.host = host;
+ this.port = port;
+ this.contextPath = contextPath;
+ this.webPath = webPath;
+ }
+
+ /**
+ * This is a Blocking call and will wait till the server is Started
+ *
+ * @throws Exception
+ */
+ public void start() throws Exception {
+ Thread t = new Thread() {
+ public void run() {
+ server = new Server();
+ SocketConnector connector = new SocketConnector();
+ connector.setPort(port);
+ server.setConnectors(new Connector[]{connector});
+
+ context = new WebAppContext();
+ context.setServer(server);
+ context.setContextPath(contextPath);
+ //context.setWar(warFilePath);
+
+ //Note: Set WAR assumes all resources etc in place like
genuine WAR,
+ //in our case resources scattered across so use the
following instead:
+ context.setResourceBase("src/main/resources");
+ context.setDescriptor(webPath + "/web.xml");
+ server.addHandler(context);
+
+ Thread monitor = new MonitorThread(host, stopPort, server);
+ monitor.setDaemon(true);
+ monitor.start();
+
+ try {
+ server.start();
+ server.setStopAtShutdown(true);
+ server.join();
+// while (context.isStarting()) {
+// Thread.sleep(1000);
+// };
+ } catch (Exception e) {
+ throw new RuntimeException(e);
+ }
+ }
+ };
+
+ t.setDaemon(true);
+ t.start();
+
+ while (server == null || !server.isStarted()) {/* Block till
started */
+ log.info("Waiting server starting.");
+ Thread.sleep(1000);
+ }
+
+ log.info("[start] Started Server @ " + host + ":" + port);
+ log.info("[start] Server Ready & Running - " + server.isRunning());
+ }
+
+ public void stop() throws Exception {
+ Socket s = new Socket(InetAddress.getByName(host), stopPort);
+ OutputStream out = s.getOutputStream();
+ log.info("[stop] sending jetty stop request @ " + host + ":" +
stopPort);
+ out.write(("\r\n").getBytes());
+ out.flush();
+ s.close();
+
+ if (server != null && server.isStarted()) {
+ server.stop();
+ }
+ }
+
+ private static final class MonitorThread extends Thread {
+
+ private ServerSocket socket;
+ private Server server;
+ private int stopPort;
+ private String host;
+
+ public MonitorThread(String host, int stopPort, Server server) {
+ this.server = server;
+ this.stopPort = stopPort;
+ this.host = host;
+
+ setDaemon(true);
+ setName("StopMonitor");
+ try {
+ socket = new ServerSocket(stopPort, 1,
InetAddress.getByName(host));
+ } catch (Exception e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+ @Override
+ public void run() {
+ log.info("[run] Running Jetty Stop Thread");
+ Socket accept;
+ try {
+ accept = socket.accept();
+ BufferedReader reader = new BufferedReader(new
InputStreamReader(accept.
+ getInputStream()));
+ reader.readLine();
+ log.info("[run] Stopping embedded Jetty Server");
+ server.stop();
+ accept.close();
+ socket.close();
+ } catch (Exception e) {
+ throw new RuntimeException(e);
+ }
+ }
+ }
+}
=======================================
--- /dev/null
+++
/plugins/parancoe-plugin-configuration/src/test/java/org/parancoe/plugin/configuration/TestContextListener.java
Mon Oct 29 01:24:47 2012
@@ -0,0 +1,56 @@
+/**
+ * Copyright (C) 2006-2012 The Parancoe Team <in...@parancoe.org>
+ *
+ * This file is part of Parancoe Plugin Configuration.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.parancoe.plugin.configuration;
+
+import java.util.ArrayList;
+import java.util.List;
+import org.parancoe.web.ContextListener;
+import org.springframework.web.context.WebApplicationContext;
+import org.springframework.web.context.support.XmlWebApplicationContext;
+
+/**
+ * Context listener for the test web application.
+ *
+ * @author Lucio Benfante <lu...@benfante.com>
+ */
+public class TestContextListener extends ContextListener {
+
+ @Override
+ protected void loadApplicationContext() {
+ List<String> config = new ArrayList<String>();
+ // generici
+
config.add("classpath:org/lambico/spring/dao/hibernate/genericDao.xml");
+
config.add("classpath:org/lambico/spring/dao/hibernate/applicationContextBase.xml");
+ config.add("classpath:org/parancoe/web/parancoeBase.xml");
+ config.add("classpath:spring-test.xml");
+ config.add("classpath*:parancoe-plugin.xml");
+ config.add("classpath*:applicationContext-plugin.xml");
+
config.add("classpath:org/parancoe/plugin/configuration/restful-services.xml");
+ XmlWebApplicationContext ctx = new XmlWebApplicationContext();
+ ctx.setServletContext(servletContext);
+ ctx.setConfigLocations(config.toArray(new String[config.size()]));
+ ctx.refresh();
+
+
servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE,
ctx);
+ applicationContext = ctx;
+
+ populateDaoMap(ctx);
+ }
+
+
+}
=======================================
--- /dev/null
+++
/plugins/parancoe-plugin-configuration/src/test/resources/org/parancoe/plugin/configuration/rest-test-client.xml
Mon Oct 29 01:24:47 2012
@@ -0,0 +1,49 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+
+ Copyright (C) 2006-2012 The Parancoe Team <in...@parancoe.org>
+
+ This file is part of Parancoe Plugin Configuration.
+
+ Licensed under the Apache License, Version 2.0 (the "License");
+ you may not use this file except in compliance with the License.
+ You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+
+-->
+<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:jaxrs="http://cxf.apache.org/jaxrs"
xmlns:cxf="http://cxf.apache.org/core"
+ xsi:schemaLocation = "http://www.springframework.org/schema/beans
+ http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
+ http://www.springframework.org/schema/context
+
http://www.springframework.org/schema/context/spring-context-3.0.xsd
+ http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd
+ http://cxf.apache.org/jaxrs
http://cxf.apache.org/schemas/jaxrs.xsd">
+
+ <!-- <import
resource="classpath:META-INF/cxf/cxf-extension-http-jetty.xml" /> -->
+ <jaxrs:client id="testclient"
address="http://localhost:9091/testContext/services/plugin"
serviceClass="org.parancoe.plugin.configuration.bo.ConfigurationService">
<!-- Interface matching service class -->
+ <jaxrs:headers>
+ <entry key="Accept" value="application/json"></entry>
+ </jaxrs:headers>
+ <jaxrs:providers>
+ <bean
class="com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider"/>
+ </jaxrs:providers>
+ </jaxrs:client>
+
+ <jaxrs:client id="testclientxml"
address="http://localhost:9091/testContext/services/plugin"
serviceClass="org.parancoe.plugin.configuration.bo.ConfigurationService">
<!-- Interface matching service class -->
+ <jaxrs:headers>
+ <entry key="Accept" value="application/xml"></entry>
+ </jaxrs:headers>
+ <jaxrs:providers>
+ </jaxrs:providers>
+ </jaxrs:client>
+
+</beans>
=======================================
--- /dev/null
+++
/plugins/parancoe-plugin-configuration/src/test/resources/org/parancoe/plugin/configuration/restful-services.xml
Mon Oct 29 01:24:47 2012
@@ -0,0 +1,100 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+
+ Copyright (C) 2006-2012 The Parancoe Team <in...@parancoe.org>
+
+ This file is part of Parancoe Plugin Configuration.
+
+ Licensed under the Apache License, Version 2.0 (the "License");
+ you may not use this file except in compliance with the License.
+ You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+
+-->
+<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
+ xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
+ xmlns:jaxrs="http://cxf.apache.org/jaxrs"
xmlns:cxf="http://cxf.apache.org/core"
+ xsi:schemaLocation="http://www.springframework.org/schema/beans
+ http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
+ http://www.springframework.org/schema/aop
+ http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
+ http://www.springframework.org/schema/tx
+ http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
+ http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd
+ http://cxf.apache.org/jaxrs
http://cxf.apache.org/schemas/jaxrs.xsd">
+
+ <import resource="classpath:META-INF/cxf/cxf.xml"/>
+ <!-- <import
resource="classpath:META-INF/cxf/cxf-extension-jaxrs-binding.xml" /> -->
+ <import resource="classpath:META-INF/cxf/cxf-servlet.xml"/>
+
+ <bean id="logInbound"
class="org.apache.cxf.interceptor.LoggingInInterceptor"/>
+ <bean id="logOutbound"
class="org.apache.cxf.interceptor.LoggingOutInterceptor"/>
+ <!--
+ logging
+ -->
+ <cxf:bus>
+ <cxf:inInterceptors>
+ <ref bean="logInbound"/>
+ </cxf:inInterceptors>
+ <cxf:outInterceptors>
+ <ref bean="logOutbound"/>
+ </cxf:outInterceptors>
+ <cxf:inFaultInterceptors>
+ <ref bean="logOutbound"/>
+ </cxf:inFaultInterceptors>
+ </cxf:bus>
+ <jaxrs:server id="genericObject_jaxrsservice" address="/plugin">
+ <jaxrs:serviceBeans>
+ <ref bean="configurationService"/>
+ </jaxrs:serviceBeans>
+ <jaxrs:providers>
+ <bean
class="com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider"/>
+ <bean
class="org.apache.cxf.jaxrs.provider.JAXBElementProvider"/>
+ </jaxrs:providers>
+ </jaxrs:server>
+
+ <bean id="configurationService"
class="org.parancoe.plugin.configuration.bo.ConfigurationManager"/>
+
+ <bean id="testConfiguration"
class="org.parancoe.plugin.configuration.ConfigurationCollection">
+ <property name="categories">
+ <list>
+ <bean
class="org.parancoe.plugin.configuration.po.Category">
+ <property name="name" value="first_category"/>
+ <property name="description" value="First sample
category"/>
+ <property name="properties">
+ <list>
+ <bean
class="org.parancoe.plugin.configuration.po.Property">
+ <property name="name"
value="first_property"/>
+ <property name="description" value="First
sample property"/>
+ <property name="type" value="STRING"/>
+ <property name="value" value="First
property value"/>
+ </bean>
+ <bean
class="org.parancoe.plugin.configuration.po.Property">
+ <property name="name"
value="second_property"/>
+ <property name="description" value="Second
sample property"/>
+ <property name="type" value="STRING"/>
+ <property name="value" value="Second
property value"/>
+ </bean>
+ </list>
+ </property>
+ </bean>
+ <bean
class="org.parancoe.plugin.configuration.po.Category">
+ <property name="name" value="second_category"/>
+ <property name="description" value="Second sample
category"/>
+ <property name="properties">
+ <list></list>
+ </property>
+ </bean>
+ </list>
+ </property>
+ </bean>
+
+</beans>
+
=======================================
--- /dev/null
+++
/plugins/parancoe-plugin-configuration/src/test/webapp/WEB-INF/database.xml
Mon Oct 29 01:24:47 2012
@@ -0,0 +1,51 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+
+ Copyright (C) 2006-2012 The Parancoe Team <in...@parancoe.org>
+
+ This file is part of Parancoe Plugin Configuration.
+
+ Licensed under the Apache License, Version 2.0 (the "License");
+ you may not use this file except in compliance with the License.
+ You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+
+-->
+<beans xmlns="http://www.springframework.org/schema/beans"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
+
+ <!-- this conf file will be automatically merged with parancoe's
parancoeBase.xml -->
+ <bean id="dataSource"
class="org.springframework.jndi.JndiObjectFactoryBean">
+ <property name="jndiName" value="java:comp/env/jdbc/dataSource"/>
+ </bean>
+
+ <bean id="sessionFactory" parent="abstractSessionFactory">
+ <property name="hibernateProperties">
+ <props merge="true">
+
+ <!-- <prop
key="hibernate.dialect">org.hibernate.dialect.HSQLDialect</prop> -->
+
+
+ <prop
key="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</prop>
+
+ <prop key="hibernate.hbm2ddl.auto">create</prop>
+ <prop key="hibernate.show_sql">true</prop>
+ </props>
+ </property>
+ <property name="eventListeners">
+ <map>
+ <entry key="merge">
+ <bean
class="org.springframework.orm.hibernate3.support.IdTransferringMergeEventListener"/>
+ </entry>
+ </map>
+ </property>
+ </bean>
+</beans>
=======================================
--- /dev/null
+++ /plugins/parancoe-plugin-configuration/src/test/webapp/WEB-INF/web.xml
Mon Oct 29 01:24:47 2012
@@ -0,0 +1,62 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+
+ Copyright (C) 2006-2012 The Parancoe Team <in...@parancoe.org>
+
+ This file is part of Parancoe Plugin Configuration.
+
+ Licensed under the Apache License, Version 2.0 (the "License");
+ you may not use this file except in compliance with the License.
+ You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+
+-->
+<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
+ <display-name>JAX-RS Simple Service</display-name>
+ <description>JAX-RS Simple Service</description>
+ <!--
+ <context-param>
+ <param-name>contextConfigLocation</param-name>
+
<param-value>classpath:org/lambico/spring/dao/hibernate/genericDao.xml,classpath:org/lambico/spring/dao/hibernate/applicationContextBase.xml,classpath:org/parancoe/web/parancoeBase.xml,classpath:spring-test.xml,classpath*:parancoe-plugin.xml,classpath*:applicationContext-plugin.xml,classpath:org/parancoe/plugin/configuration/restful-services.xml</param-value>
+ </context-param>
+ -->
+ <listener>
+
<listener-class>org.parancoe.plugin.configuration.TestContextListener</listener-class>
+ </listener>
+<!--
+ <listener>
+ <listener-class>
+ org.springframework.web.context.ContextLoaderListener
+ </listener-class>
+ </listener>
+-->
+ <listener>
+
<listener-class>org.parancoe.plugin.configuration.PopulateInitialDataContextListener</listener-class>
+ </listener>
+ <servlet>
+ <servlet-name>CXFServlet</servlet-name>
+ <servlet-class>
+ org.apache.cxf.transport.servlet.CXFServlet
+ </servlet-class>
+ <init-param>
+ <param-name>redirects-list</param-name>
+ <param-value>/forms/.*</param-value>
+ </init-param>
+ <init-param>
+ <param-name>redirect-servlet-name</param-name>
+ <param-value>jsp</param-value>
+ </init-param>
+ <load-on-startup>1</load-on-startup>
+ </servlet>
+ <servlet-mapping>
+ <servlet-name>CXFServlet</servlet-name>
+ <url-pattern>/services/*</url-pattern>
+ </servlet-mapping>
+</web-app>
=======================================
--- /parancoe-web/src/main/java/org/parancoe/web/ContextListener.java Thu
Apr 26 07:55:31 2012
+++ /parancoe-web/src/main/java/org/parancoe/web/ContextListener.java Mon
Oct 29 01:24:47 2012
@@ -43,9 +43,10 @@
*/
public class ContextListener implements ServletContextListener {
private static final Logger log =
Logger.getLogger(ContextListener.class);
- private ServletContext servletContext;
- private XmlWebApplicationContext applicationContext;
+ protected ServletContext servletContext;
+ protected XmlWebApplicationContext applicationContext;

+ @Override
public void contextInitialized(ServletContextEvent evt) {
try {
this.servletContext = evt.getServletContext();
@@ -68,7 +69,7 @@
* load the ApplicationContext mixing the base parancoe
* files and the application specific configuration
*/
- private void loadApplicationContext() {
+ protected void loadApplicationContext() {
List<String> config = new ArrayList<String>();
// generici

config.add("classpath:org/lambico/spring/dao/hibernate/genericDao.xml");
@@ -97,7 +98,7 @@
/**
* Populate the "daoMap" bean with the DAOs defined in the context.
*/
- private void populateDaoMap(XmlWebApplicationContext ctx) {
+ protected void populateDaoMap(XmlWebApplicationContext ctx) {
Map daoMap = (Map) ctx.getBean("daoMap");
Map daos = DaoUtils.getDaos(ctx);
daoMap.putAll(daos);
=======================================
--- /plugins/parancoe-plugin-configuration/pom.xml Mon Apr 30 00:39:28 2012
+++ /plugins/parancoe-plugin-configuration/pom.xml Mon Oct 29 01:24:47 2012
@@ -20,10 +20,35 @@
</build>
<dependencies>
<dependency>
+ <groupId>org.apache.cxf</groupId>
+ <artifactId>cxf-rt-frontend-jaxrs</artifactId>
+ <version>2.6.2</version>
+ </dependency>
+ <dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.4</version>
<scope>test</scope>
</dependency>
+ <dependency>
+ <groupId>org.mortbay.jetty</groupId>
+ <artifactId>jetty</artifactId>
+ <version>6.1.26</version>
+ <scope>test</scope>
+ <type>jar</type>
+ </dependency>
+ <dependency>
+ <artifactId>junit</artifactId>
+ <groupId>junit</groupId>
+ <type>jar</type>
+ <version>4.10</version>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>com.fasterxml.jackson.jaxrs</groupId>
+ <artifactId>jackson-jaxrs-json-provider</artifactId>
+ <version>2.0.4</version>
+ <type>jar</type>
+ </dependency>
</dependencies>
</project>
=======================================
---
/plugins/parancoe-plugin-configuration/src/main/java/org/parancoe/plugin/configuration/ConfigurationCollection.java
Thu May 3 06:29:42 2012
+++
/plugins/parancoe-plugin-configuration/src/main/java/org/parancoe/plugin/configuration/ConfigurationCollection.java
Mon Oct 29 01:24:47 2012
@@ -18,6 +18,7 @@
package org.parancoe.plugin.configuration;

import java.util.List;
+import javax.xml.bind.annotation.XmlRootElement;
import org.parancoe.plugin.configuration.po.Category;

/**
@@ -26,10 +27,18 @@
*
* @author Lucio Benfante <lu...@benfante.com>
*/
+@XmlRootElement
public class ConfigurationCollection {

private List<Category> categories;

+ public ConfigurationCollection() {
+ }
+
+ public ConfigurationCollection(List<Category> categories) {
+ this.categories = categories;
+ }
+
public List<Category> getCategories() {
return categories;
}
=======================================
---
/plugins/parancoe-plugin-configuration/src/main/java/org/parancoe/plugin/configuration/bo/ConfigurationManager.java
Thu May 3 06:29:42 2012
+++
/plugins/parancoe-plugin-configuration/src/main/java/org/parancoe/plugin/configuration/bo/ConfigurationManager.java
Mon Oct 29 01:24:47 2012
@@ -19,12 +19,13 @@

import java.util.List;
import javax.annotation.Resource;
-import org.apache.log4j.Logger;
import org.parancoe.plugin.configuration.ConfigurationCollection;
+import org.parancoe.plugin.configuration.PropertyCollection;
import org.parancoe.plugin.configuration.dao.CategoryDao;
import org.parancoe.plugin.configuration.dao.PropertyDao;
import org.parancoe.plugin.configuration.po.Category;
import org.parancoe.plugin.configuration.po.Property;
+import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@@ -35,9 +36,9 @@
* @author Lucio Benfante <lu...@benfante.com>
*/
@Service
-public class ConfigurationManager {
+public class ConfigurationManager implements ConfigurationService {

- private static final Logger log =
Logger.getLogger(ConfigurationManager.class);
+ private static final org.slf4j.Logger log =
LoggerFactory.getLogger(ConfigurationManager.class);
@Resource
private CategoryDao categoryDao;
@Resource
@@ -51,15 +52,46 @@
* @return A property. null if the category or the property are not
found.
*/
@Transactional(readOnly = true)
+ @Override
public Property getProperty(String categoryName, String propertyName) {
Property result = null;
Category category = categoryDao.findByName(categoryName);
if (category != null) {
+ log.debug("Category {} found.", categoryName);
result = propertyDao.findByNameAndCategoryId(propertyName,
category.getId());
+ if (log.isDebugEnabled()) {
+ if (result == null) {
+ log.debug("Property {} not found.", propertyName);
+ } else {
+ log.debug("Property {} found.", propertyName);
+ }
+ }
+ } else {
+ log.debug("Category {} not found.", categoryName);
}
return result;
}

+ /**
+ * Return all properties of a category.
+ *
+ * @param categoryName The category name.
+ * @return A property collection. null if the category is not found.
+ */
+ @Transactional(readOnly = true)
+ @Override
+ public PropertyCollection getProperties(String categoryName) {
+ List<Property> result = null;
+ Category category = categoryDao.findByName(categoryName);
+ if (category != null) {
+ log.debug("Category {} found.", categoryName);
+ result = propertyDao.findByCategoryId(category.getId());
+ } else {
+ log.debug("Category {} not found.", categoryName);
+ }
+ return new PropertyCollection(result);
+ }
+
/**
* Load a property.
*
@@ -67,6 +99,7 @@
* @return A property. null if the property doesn't exist.
*/
@Transactional(readOnly = true)
+ @Override
public Property getProperty(Long id) {
return propertyDao.get(id);
}
@@ -81,12 +114,23 @@
return categoryDao.findByOrderByName();
}

+ /**
+ * Load all configuration categories.
+ *
+ * @return The configuration categories.
+ */
+ @Transactional(readOnly = true)
+ public ConfigurationCollection getConfiguration() {
+ return new ConfigurationCollection(loadCategories());
+ }
+
/**
* Store a property.
*
* @param property The property to store.
*/
@Transactional
+ @Override
public void store(Property property) {
propertyDao.store(property);
}
@@ -98,6 +142,7 @@
* @param value The configuration collection to initialize.
*/
@Transactional
+ @Override
public void initializeConfiguration(ConfigurationCollection
configurationCollection) {
for (Category category : configurationCollection.getCategories()) {
Category dbCategory =
categoryDao.findByName(category.getName());
=======================================
---
/plugins/parancoe-plugin-configuration/src/main/java/org/parancoe/plugin/configuration/dao/PropertyDao.java
Mon Apr 30 00:39:28 2012
+++
/plugins/parancoe-plugin-configuration/src/main/java/org/parancoe/plugin/configuration/dao/PropertyDao.java
Mon Oct 29 01:24:47 2012
@@ -17,6 +17,7 @@
*/
package org.parancoe.plugin.configuration.dao;

+import java.util.List;
import org.lambico.dao.generic.Dao;
import org.lambico.dao.generic.GenericDao;
import org.parancoe.plugin.configuration.po.Property;
@@ -24,4 +25,5 @@
@Dao(entity=Property.class)
public interface PropertyDao extends GenericDao<Property, Long> {
Property findByNameAndCategoryId(String name, Long categoryId);
+ List<Property> findByCategoryId(Long categoryId);
}
=======================================
---
/plugins/parancoe-plugin-configuration/src/main/java/org/parancoe/plugin/configuration/po/Category.java
Thu May 3 06:29:42 2012
+++
/plugins/parancoe-plugin-configuration/src/main/java/org/parancoe/plugin/configuration/po/Category.java
Mon Oct 29 01:24:47 2012
@@ -26,6 +26,7 @@
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;
import javax.persistence.Table;
+import javax.xml.bind.annotation.XmlTransient;
import org.lambico.po.hibernate.EntityBase;

/**
@@ -84,6 +85,7 @@

@OneToMany(mappedBy = "category", fetch= FetchType.EAGER)
@OrderBy("name ASC")
+ @XmlTransient
public List<Property> getProperties() {
return properties;
}
=======================================
---
/plugins/parancoe-plugin-configuration/src/main/java/org/parancoe/plugin/configuration/po/Property.java
Tue May 22 22:03:14 2012
+++
/plugins/parancoe-plugin-configuration/src/main/java/org/parancoe/plugin/configuration/po/Property.java
Mon Oct 29 01:24:47 2012
@@ -17,6 +17,9 @@
*/
package org.parancoe.plugin.configuration.po;

+import com.fasterxml.jackson.annotation.JsonIdentityInfo;
+import com.fasterxml.jackson.annotation.JsonIgnore;
+import com.fasterxml.jackson.annotation.ObjectIdGenerators;
import java.math.BigDecimal;
import javax.persistence.Column;
import javax.persistence.Entity;
@@ -28,6 +31,7 @@
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.persistence.Transient;
+import javax.xml.bind.annotation.XmlRootElement;
import org.lambico.po.hibernate.EntityBase;

/**
@@ -39,8 +43,13 @@
@Table(name = "PLUGIN_CONFIGURATION_PROPERTY")
@NamedQueries({
@NamedQuery(name = "Property.findByNameAndCategoryId", query =
- "from Property p where p.name = ? and p.category.id = ?")
+ "from Property p where p.name = ? and p.category.id = ? order by
p.category.name, p.name"),
+ @NamedQuery(name = "Property.findByCategoryId", query =
+ "from Property p where p.category.id = ? order by p.name")
})
+@JsonIdentityInfo(generator =
ObjectIdGenerators.IntSequenceGenerator.class,
+ property = "@id")
+@XmlRootElement
public class Property extends EntityBase {

private String name;
@@ -180,16 +189,19 @@
}

@Transient
+ @JsonIgnore
public Integer getValueAsInteger() throws NumberFormatException {
return Integer.valueOf(this.getValue());
}

@Transient
+ @JsonIgnore
public BigDecimal getValueAsReal() {
return new BigDecimal(this.getValue());
}

@Transient
+ @JsonIgnore
public Boolean getValueAsBoolean() {
return Boolean.valueOf(this.getValue());
}
=======================================
---
/plugins/parancoe-plugin-configuration/src/test/resources/log4j.properties
Mon Apr 30 00:39:28 2012
+++
/plugins/parancoe-plugin-configuration/src/test/resources/log4j.properties
Mon Oct 29 01:24:47 2012
@@ -7,3 +7,4 @@
log4j.logger.org.parancoe=DEBUG
log4j.logger.org.lambico=DEBUG
log4j.logger.org.parancoe.plugin.configuration=DEBUG
+log4j.logger.org.apache.cxf=DEBUG
=======================================
---
/plugins/parancoe-plugin-configuration/src/test/resources/spring-test.xml
Thu Apr 26 07:55:58 2012
+++
/plugins/parancoe-plugin-configuration/src/test/resources/spring-test.xml
Mon Oct 29 01:24:47 2012
@@ -47,7 +47,7 @@
</prop>
<prop key="hibernate.hbm2ddl.auto">create
</prop>
- <prop key="hibernate.show_sql">false
+ <prop key="hibernate.show_sql">true
</prop>
</props>
</property>
Reply all
Reply to author
Forward
0 new messages