001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.cocoon.auth;
018:
019: import java.util.HashMap;
020: import java.util.Map;
021:
022: import org.apache.avalon.framework.activity.Disposable;
023: import org.apache.avalon.framework.configuration.Configurable;
024: import org.apache.avalon.framework.configuration.Configuration;
025: import org.apache.avalon.framework.configuration.ConfigurationException;
026: import org.apache.avalon.framework.logger.AbstractLogEnabled;
027: import org.apache.avalon.framework.service.ServiceException;
028: import org.apache.avalon.framework.service.ServiceManager;
029: import org.apache.avalon.framework.service.Serviceable;
030: import org.apache.avalon.framework.thread.ThreadSafe;
031: import org.apache.cocoon.auth.impl.AnonymousSecurityHandler;
032:
033: /**
034: * This is the default implementation for an {@link Application}.
035: *
036: * @version $Id: StandardApplication.java 433543 2006-08-22 06:22:54Z crossley $
037: */
038: public class StandardApplication extends AbstractLogEnabled implements
039: Application, Configurable, Serviceable, Disposable, ThreadSafe {
040:
041: /** This prefix is used to lookup security handlers. */
042: protected static final String HANDLER_CONFIG_PREFIX = SecurityHandler.class
043: .getName() + '/';
044: /** This prefix is used to lookup application stores. */
045: protected static final String STORE_CONFIG_PREFIX = ApplicationStore.class
046: .getName() + '/';
047:
048: /** The service manager. */
049: protected ServiceManager manager;
050:
051: /** The security handler. */
052: protected SecurityHandler handler;
053:
054: /** Attributes. */
055: protected final Map attributes = new HashMap();
056:
057: /** Application store. */
058: protected ApplicationStore store;
059:
060: /**
061: * @see org.apache.avalon.framework.service.Serviceable#service(org.apache.avalon.framework.service.ServiceManager)
062: */
063: public void service(final ServiceManager aManager)
064: throws ServiceException {
065: this .manager = aManager;
066: }
067:
068: /**
069: * @see org.apache.avalon.framework.configuration.Configurable#configure(org.apache.avalon.framework.configuration.Configuration)
070: */
071: public void configure(final Configuration conf)
072: throws ConfigurationException {
073: String handlerName = conf
074: .getAttribute("security-handler", null);
075: String storeName = conf.getAttribute("store", null);
076: try {
077: if (handlerName == null) {
078: this .handler = new AnonymousSecurityHandler();
079: } else {
080: if (!handlerName.startsWith(HANDLER_CONFIG_PREFIX)) {
081: handlerName = HANDLER_CONFIG_PREFIX + handlerName;
082: }
083: this .handler = (SecurityHandler) this .manager
084: .lookup(handlerName);
085: }
086: if (storeName != null) {
087: if (!storeName.startsWith(STORE_CONFIG_PREFIX)) {
088: storeName = STORE_CONFIG_PREFIX + storeName;
089: }
090: this .store = (ApplicationStore) this .manager
091: .lookup(storeName);
092: }
093: } catch (ServiceException se) {
094: throw new ConfigurationException(
095: "Unable to look up component.", se);
096: }
097: this .configureAttributes(conf);
098: }
099:
100: /**
101: * This method is invoked during configuration of the application. The
102: * default behaviour is to add all children of the configuration object
103: * as key value pairs. The name of the child is the key, and the value
104: * of the tag is the value (as a string).
105: * Subclasses can override this method, if a different/additional
106: * behaviour is wanted.
107: * @param conf The application configuration.
108: */
109: protected void configureAttributes(final Configuration conf) {
110: Configuration[] children = conf.getChildren();
111: for (int i = 0; i < children.length; i++) {
112: final String name = children[i].getName();
113: final String value = children[i].getValue(null);
114: if (value != null && value.trim().length() > 0) {
115: this .setAttribute(name, value.trim());
116: }
117: }
118: }
119:
120: /**
121: * @see org.apache.avalon.framework.activity.Disposable#dispose()
122: */
123: public void dispose() {
124: if (this .manager != null) {
125: this .manager.release(this .store);
126: if (!(this .handler instanceof AnonymousSecurityHandler)) {
127: this .manager.release(this .handler);
128: }
129: this .store = null;
130: this .handler = null;
131: this .manager = null;
132: }
133: }
134:
135: /**
136: * @see org.apache.cocoon.auth.Application#getSecurityHandler()
137: */
138: public SecurityHandler getSecurityHandler() {
139: return this .handler;
140: }
141:
142: /**
143: * @see org.apache.cocoon.auth.Application#getApplicationStore()
144: */
145: public ApplicationStore getApplicationStore() {
146: return this .store;
147: }
148:
149: /**
150: * @see org.apache.cocoon.auth.Application#setAttribute(java.lang.String, java.lang.Object)
151: */
152: public void setAttribute(final String key, final Object value) {
153: this .attributes.put(key, value);
154: }
155:
156: /**
157: * @see org.apache.cocoon.auth.Application#removeAttribute(java.lang.String)
158: */
159: public void removeAttribute(final String key) {
160: this .attributes.remove(key);
161: }
162:
163: /**
164: * @see org.apache.cocoon.auth.Application#getAttribute(java.lang.String)
165: */
166: public Object getAttribute(final String key) {
167: return this .attributes.get(key);
168: }
169:
170: /**
171: * @see org.apache.cocoon.auth.Application#userDidLogin(org.apache.cocoon.auth.User, java.util.Map)
172: */
173: public void userDidLogin(final User user, final Map context) {
174: // nothing to do here
175: }
176:
177: /**
178: * @see org.apache.cocoon.auth.Application#userWillLogout(org.apache.cocoon.auth.User, java.util.Map)
179: */
180: public void userWillLogout(final User user, final Map context) {
181: // nothing to do here
182: }
183:
184: /**
185: * @see org.apache.cocoon.auth.Application#userIsAccessing(org.apache.cocoon.auth.User)
186: */
187: public void userIsAccessing(final User user) {
188: // nothing to do here
189: }
190: }
|