1 16 17 package org.apache.cocoon.components.slide.impl; 18 19 import java.util.Hashtable ; 20 21 import org.apache.avalon.framework.activity.Disposable; 22 import org.apache.avalon.framework.activity.Initializable; 23 import org.apache.avalon.framework.configuration.Configurable; 24 import org.apache.avalon.framework.configuration.Configuration; 25 import org.apache.avalon.framework.configuration.ConfigurationException; 26 import org.apache.avalon.framework.configuration.SAXConfigurationHandler; 27 import org.apache.avalon.framework.context.ContextException; 28 import org.apache.avalon.framework.context.Contextualizable; 29 import org.apache.avalon.framework.logger.AbstractLogEnabled; 30 import org.apache.avalon.framework.service.ServiceException; 31 import org.apache.avalon.framework.service.ServiceManager; 32 import org.apache.avalon.framework.service.Serviceable; 33 import org.apache.avalon.framework.thread.ThreadSafe; 34 import org.apache.cocoon.Constants; 35 import org.apache.cocoon.components.slide.SlideRepository; 36 import org.apache.cocoon.environment.Context; 37 import org.apache.excalibur.source.Source; 38 import org.apache.excalibur.source.SourceResolver; 39 import org.apache.excalibur.xml.sax.SAXParser; 40 import org.apache.slide.common.Domain; 41 import org.apache.slide.common.EmbeddedDomain; 42 import org.apache.slide.common.NamespaceAccessToken; 43 import org.xml.sax.InputSource ; 44 45 50 public class SlideRepositoryImpl extends AbstractLogEnabled 51 implements SlideRepository, Contextualizable, Serviceable, Configurable, 52 Initializable, Disposable, ThreadSafe { 53 54 private ServiceManager manager; 55 56 60 private EmbeddedDomain domain = null; 61 62 private String file; 63 private String contextpath; 64 private String workdir; 65 66 67 public SlideRepositoryImpl() { 68 } 69 70 public void service(ServiceManager manager) throws ServiceException { 71 this.manager = manager; 72 } 73 74 public void contextualize(org.apache.avalon.framework.context.Context context) 75 throws ContextException { 76 Context ctx = ((Context) context.get(Constants.CONTEXT_ENVIRONMENT_CONTEXT)); 77 this.contextpath = ctx.getRealPath("/"); 78 this.workdir = context.get(Constants.CONTEXT_WORK_DIR).toString(); 79 } 80 81 public void configure(Configuration configuration) 82 throws ConfigurationException { 83 84 this.file = configuration.getAttribute("file", "WEB-INF/slide.xconf"); 85 } 86 87 public void initialize() throws Exception { 88 89 if (Domain.isInitialized()) { 90 return; 91 } 92 93 getLogger().info("Initializing domain."); 94 95 this.domain = new EmbeddedDomain(); 96 domain.setLogger(new SlideLoggerAdapter(getLogger())); 99 100 SourceResolver resolver = null; 101 SAXParser parser = null; 102 Source source = null; 103 Configuration configuration = null; 104 try { 105 resolver = (SourceResolver) this.manager.lookup(SourceResolver.ROLE); 106 107 parser = (SAXParser) this.manager.lookup(SAXParser.ROLE); 108 SAXConfigurationHandler confighandler = new SAXConfigurationHandler(); 109 110 source = resolver.resolveURI(this.file); 111 parser.parse(new InputSource (source.getInputStream()),confighandler); 112 configuration = confighandler.getConfiguration(); 113 114 } finally { 115 if (source != null) { 116 resolver.release(source); 117 } 118 if (parser != null) { 119 this.manager.release(parser); 120 } 121 if (resolver != null) { 122 this.manager.release(resolver); 123 } 124 } 125 126 Configuration[] parameters = configuration.getChildren("parameter"); 127 Hashtable table = new Hashtable (); 128 for (int i = 0; i < parameters.length; i++) { 129 String name = parameters[i].getAttribute("name"); 130 table.put(name, parameters[i].getValue("")); 131 } 132 table.put("contextpath", this.contextpath); 133 table.put("workdir", this.workdir); 134 this.domain.setParameters(table); 135 136 domain.setDefaultNamespace(configuration.getAttribute("default","cocoon")); 137 Configuration[] namespace = configuration.getChildren("namespace"); 138 139 for (int i = 0; i< namespace.length; i++) { 140 String name = namespace[i].getAttribute("name"); 141 Configuration definition = namespace[i].getChild("definition"); 142 Configuration config = namespace[i].getChild("configuration"); 143 Configuration data = namespace[i].getChild("data"); 144 145 getLogger().info("Initializing namespace: " + name); 146 147 domain.addNamespace(name, 148 new SlideLoggerAdapter(getLogger().getChildLogger(name)), 149 new SlideConfigurationAdapter(definition), 150 new SlideConfigurationAdapter(config), 151 new SlideConfigurationAdapter(data)); 152 153 } 154 155 domain.start(); 156 Domain.setInitialized(true); 157 } 158 159 public void dispose() { 160 try { 161 domain.stop(); 162 } catch (Exception e) { 163 getLogger().error("Could not stop domain", e); 164 } 165 } 166 167 172 public NamespaceAccessToken getDefaultNamespaceToken() { 173 174 if (domain != null) { 175 return this.domain.getNamespaceToken(this.domain.getDefaultNamespace()); 176 } 177 178 return Domain.accessNamespace(null, Domain.getDefaultNamespace()); 179 } 180 181 187 public NamespaceAccessToken getNamespaceToken(String namespaceName) { 188 189 if (namespaceName == null) { 190 return getDefaultNamespaceToken(); 191 } 192 193 if (domain != null) { 194 return this.domain.getNamespaceToken(namespaceName); 195 } 196 197 return Domain.accessNamespace(null, namespaceName); 198 } 199 } 200 | Popular Tags |