001: /*
002: * Copyright 2005-2007 Noelios Consulting.
003: *
004: * The contents of this file are subject to the terms of the Common Development
005: * and Distribution License (the "License"). You may not use this file except in
006: * compliance with the License.
007: *
008: * You can obtain a copy of the license at
009: * http://www.opensource.org/licenses/cddl1.txt See the License for the specific
010: * language governing permissions and limitations under the License.
011: *
012: * When distributing Covered Code, include this CDDL HEADER in each file and
013: * include the License file at http://www.opensource.org/licenses/cddl1.txt If
014: * applicable, add the following below this CDDL HEADER, with the fields
015: * enclosed by brackets "[]" replaced with your own identifying information:
016: * Portions Copyright [yyyy] [name of copyright owner]
017: */
018:
019: package org.restlet.ext.spring;
020:
021: import java.util.ArrayList;
022: import java.util.List;
023:
024: import org.restlet.Context;
025: import org.restlet.resource.Representation;
026: import org.springframework.beans.factory.support.PropertiesBeanDefinitionReader;
027: import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
028: import org.springframework.context.support.GenericApplicationContext;
029:
030: /**
031: * Spring application context based on a Restlet context. Here is an example
032: * illustrating the various ways to use this class:
033: *
034: * <pre>
035: * SpringContext springContext = new SpringContext(getContext());
036: * springContext.getPropertyConfigRefs().add("war://config/database.properties");
037: * springContext.getXmlConfigRefs().add("war://config/applicationContext.xml");
038: * springContext.getXmlConfigRefs().add(
039: * "file:///C/myApp/config/applicationContext.xml");
040: * springContext.getXmlConfigRefs().add(
041: * "clap://thread/config/applicationContext.xml");
042: * </pre>
043: *
044: * @author Jerome Louvel (contact@noelios.com)</a>
045: */
046: public class SpringContext extends GenericApplicationContext {
047: /** The parent Restlet context. */
048: private Context restletContext;
049:
050: /**
051: * The modifiable list of configuration URIs for beans definitions via
052: * property representations.
053: */
054: private List<String> propertyConfigRefs;
055:
056: /**
057: * The modifiable list of configuration URIs for beans definitions via XML
058: * representations.
059: */
060: private List<String> xmlConfigRefs;
061:
062: /** Indicates if the context has been already loaded. */
063: private boolean loaded;
064:
065: /**
066: * Constructor.
067: *
068: * @param restletContext
069: * The parent Restlet context.
070: */
071: public SpringContext(Context restletContext) {
072: this .restletContext = restletContext;
073: this .propertyConfigRefs = null;
074: this .xmlConfigRefs = null;
075: this .loaded = false;
076: }
077:
078: /**
079: * Returns the parent Restlet context.
080: *
081: * @return The parent Restlet context.
082: */
083: public Context getRestletContext() {
084: return this .restletContext;
085: }
086:
087: /**
088: * Returns the modifiable list of configuration URIs for beans definitions
089: * via property representations.
090: *
091: * @return The modifiable list of configuration URIs.
092: */
093: public List<String> getPropertyConfigRefs() {
094: if (this .propertyConfigRefs == null)
095: this .propertyConfigRefs = new ArrayList<String>();
096: return this .propertyConfigRefs;
097: }
098:
099: /**
100: * Returns the modifiable list of configuration URIs for beans definitions
101: * via XML representations.
102: *
103: * @return The modifiable list of configuration URIs.
104: */
105: public List<String> getXmlConfigRefs() {
106: if (this .xmlConfigRefs == null)
107: this .xmlConfigRefs = new ArrayList<String>();
108: return this .xmlConfigRefs;
109: }
110:
111: @Override
112: public void refresh() {
113: // If this context hasn't been loaded yet, read all the configurations
114: // registered
115: if (!this .loaded) {
116: Representation config = null;
117:
118: // First, read the bean definitions from properties representations
119: PropertiesBeanDefinitionReader propReader = null;
120: for (String ref : getPropertyConfigRefs()) {
121: config = getRestletContext().getDispatcher().get(ref)
122: .getEntity();
123:
124: if (config != null) {
125: propReader = new PropertiesBeanDefinitionReader(
126: this );
127: propReader.loadBeanDefinitions(new SpringResource(
128: config));
129: }
130: }
131:
132: // Then, read the bean definitions from XML representations
133: XmlBeanDefinitionReader xmlReader = null;
134: for (String ref : getXmlConfigRefs()) {
135: config = getRestletContext().getDispatcher().get(ref)
136: .getEntity();
137:
138: if (config != null) {
139: xmlReader = new XmlBeanDefinitionReader(this );
140: xmlReader
141: .setValidationMode(XmlBeanDefinitionReader.VALIDATION_XSD);
142: xmlReader.loadBeanDefinitions(new SpringResource(
143: config));
144: }
145: }
146: }
147:
148: // Now load or refresh
149: super.refresh();
150: }
151:
152: }
|