001: /*
002: * Copyright 2004 Hippo Webworks.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.apache.slide.common;
017:
018: import java.util.HashMap;
019: import java.util.Hashtable;
020: import java.util.Iterator;
021: import java.util.Map;
022: import java.util.Set;
023:
024: import org.apache.slide.util.conf.Configuration;
025: import org.apache.slide.util.logger.Logger;
026:
027: /**
028: * Utility class for accessing Slide package protected members.
029: * Replaces EmbeddedDomain.
030: *
031: * @author <a href="mailto:unico@hippo.nl">Unico Hommes</a>
032: */
033: public class SlideAccess {
034:
035: private final Map m_namespaces = new HashMap();
036:
037: public SlideAccess() {
038: }
039:
040: /**
041: * Get the number of namespaces.
042: *
043: * @return the number of namespaces.
044: */
045: public int getNumberOfNamespaces() {
046: return m_namespaces.size();
047: }
048:
049: /**
050: * Get the NamespaceAccessToken by name.
051: *
052: * @param name the namespace name.
053: * @return the access token for the namespace.
054: */
055: public NamespaceAccessToken getNamespaceToken(String name) {
056: Namespace namespace = (Namespace) m_namespaces.get(name);
057: if (namespace != null) {
058: return new NamespaceAccessTokenImpl(namespace);
059: }
060: return null;
061: }
062:
063: /**
064: * Get the Namespace by name.
065: *
066: * @param name the namespace name.
067: * @return the namespace.
068: */
069: public Namespace getNamespace(String name) {
070: Namespace namespace = (Namespace) m_namespaces.get(name);
071: return namespace;
072: }
073:
074: /**
075: * Get the Namespaces.
076: *
077: * @return the namespaces.
078: */
079: public Namespace[] getNamespaces() {
080: Namespace[] result = new Namespace[m_namespaces.size()];
081: int index = 0;
082: for (Iterator namespacesIterator = m_namespaces.values()
083: .iterator(); namespacesIterator.hasNext();) {
084: Namespace namespace = (Namespace) namespacesIterator.next();
085: result[index++] = namespace;
086: }
087: return result;
088: }
089:
090: /**
091: * Get the names of the registered namespaces.
092: *
093: * @return array of registered namespaces
094: */
095: public String[] getNamespaceNames() {
096: final Set names = m_namespaces.keySet();
097: return (String[]) names.toArray(new String[names.size()]);
098: }
099:
100: /**
101: * Setup and register a new Namespace.
102: *
103: * @param name the name of the Namespace
104: * @param logger a logger for the Namespace
105: * @param definition the namespace definition
106: * @param configuration the namespace configuration
107: * @param baseData the namespace bootstrap data
108: * @throws Exception if something goes wrong setting up the Namespace
109: */
110: public void addNamespace(String name, Logger logger,
111: Configuration definition, Configuration configuration,
112: Configuration baseData, Configuration extractors)
113: throws Exception {
114:
115: final Namespace namespace = new Namespace();
116: namespace.setName(name);
117: namespace.setLogger(logger);
118:
119: namespace.loadParameters(configuration);
120: namespace.loadDefinition(definition);
121: namespace.loadBaseData(baseData);
122: namespace.loadConfiguration(configuration);
123: namespace.loadExtractors(extractors);
124:
125: m_namespaces.put(name, namespace);
126:
127: }
128:
129: /**
130: * Unregister and disconnect a Namespace.
131: *
132: * @param name the name of the namespace to remove
133: * @throws Exception if something went wrong during disconnecting the namespaces
134: */
135: public void removeNamespace(String name) throws Exception {
136: final Namespace namespace = (Namespace) m_namespaces
137: .remove(name);
138: if (namespace != null) {
139: namespace.disconnectServices();
140: }
141: }
142:
143: /**
144: * Set the Domain -global parameters
145: *
146: * @param parameters
147: */
148: public void setParameters(Hashtable parameters) {
149: Domain.setParameters(parameters);
150: }
151:
152: /**
153: * Set the Domain -global logger
154: * @param logger
155: */
156: public void setLogger(Logger logger) {
157: Domain.setLogger(logger);
158: }
159:
160: }
|