001: package org.osbl.spring;
002:
003: import org.apache.commons.logging.Log;
004: import org.apache.commons.logging.LogFactory;
005: import org.springframework.beans.BeansException;
006: import org.springframework.beans.factory.BeanFactory;
007: import org.springframework.context.support.GenericApplicationContext;
008: import org.springframework.core.io.*;
009:
010: import org.jboss.naming.NonSerializableFactory;
011:
012: import javax.naming.*;
013: import java.util.*;
014:
015: /**
016: * @author hengels
017: */
018: public class SpringService implements SpringServiceMBean {
019: private String jndiName;
020: private String parentJndiName;
021: private String resources;
022: private static Log logger = LogFactory.getLog(SpringService.class);
023:
024: public String getJndiName() {
025: return jndiName;
026: }
027:
028: public void setJndiName(String jndiName) {
029: this .jndiName = jndiName;
030: }
031:
032: public String getParentJndiName() {
033: return parentJndiName;
034: }
035:
036: public void setParentJndiName(String parentJndiName) {
037: this .parentJndiName = parentJndiName;
038: }
039:
040: public String getResources() {
041: return resources;
042: }
043:
044: public void setResources(String resources) {
045: this .resources = resources;
046: }
047:
048: public void start() {
049: DeployedBeanFactory beanFactory = new DeployedBeanFactory();
050:
051: BeanFactory parent = getParentBeanFactory();
052: if (parent != null)
053: beanFactory.setParentBeanFactory(parent);
054:
055: StringTokenizer tokenizer = new StringTokenizer(resources, ", ");
056: for (; tokenizer.hasMoreTokens();) {
057: String resource = tokenizer.nextToken();
058: beanFactory.addResource(new ClassPathResource(resource));
059: }
060:
061: GenericApplicationContext context = null;
062: try {
063: context = new GenericApplicationContext(beanFactory);
064: context.refresh();
065: logger.info("Binding application context to jndi");
066: rebind(jndiName, context);
067: logger.info("Started spring service. Context bound to "
068: + jndiName);
069: } catch (BeansException ex) {
070: logger.error("Error while starting spring service", ex);
071: try {
072: if (context != null) {
073: context.destroy();
074: }
075: } catch (Exception bex) {
076: // Ignore it
077: }
078: throw ex;
079: }
080: }
081:
082: public void stop() {
083: try {
084: GenericApplicationContext context = (GenericApplicationContext) new InitialContext()
085: .lookup(jndiName);
086: context.destroy();
087: } catch (NamingException e) {
088: e.printStackTrace();
089: }
090: unbind(jndiName);
091: logger.info("Stopped spring service");
092: }
093:
094: public void create() {
095: logger.info("Created spring service");
096: }
097:
098: public void destroy() {
099: logger.info("Destroyed spring service");
100: }
101:
102: private BeanFactory getParentBeanFactory() {
103: if (parentJndiName == null)
104: return null;
105: try {
106: return (BeanFactory) new InitialContext()
107: .lookup(parentJndiName);
108: } catch (NamingException e) {
109: e.printStackTrace();
110: return null;
111: }
112: }
113:
114: static void rebind(String string, Object value) {
115: try {
116: Name name = new InitialContext().getNameParser("").parse(
117: string);
118: int size = name.size();
119: String atom = name.get(size - 1);
120: Context parentContext = createChildContext(name
121: .getPrefix(size - 1));
122: NonSerializableFactory.rebind(parentContext, atom, value);
123: } catch (NamingException e) {
124: throw new RuntimeException(e);
125: }
126: }
127:
128: static Context createChildContext(Name name) throws NamingException {
129: Context context = new InitialContext();
130: Context childContext = context;
131: for (int pos = 0; pos < name.size(); pos++) {
132: String string = name.get(pos);
133: try {
134: childContext = (Context) context.lookup(string);
135: } catch (NameNotFoundException e) {
136: childContext = context.createSubcontext(string);
137: }
138: context = childContext;
139: }
140: return childContext;
141: }
142:
143: public static void unbind(String name) {
144: InitialContext ctx = null;
145: try {
146: ctx = new InitialContext();
147: ctx.unbind(name);
148: NonSerializableFactory.unbind(name);
149: } catch (NamingException e) {
150: throw new RuntimeException(
151: "Unable to unbind BeanFactory from JNDI", e);
152: } finally {
153: if (ctx != null) {
154: try {
155: ctx.close();
156: } catch (Throwable ignored) {
157: }
158: }
159: }
160: }
161:
162: }
|