Author: danielf Date: Sat Mar 19 06:00:44 2005 New Revision: 158239 URL: http://svn.apache.org/viewcvs?view=rev&rev=158239 Log: Initial code for accessors
Added: cocoon/blocks/unsupported/template/trunk/java/org/apache/cocoon/components/accessor/ cocoon/blocks/unsupported/template/trunk/java/org/apache/cocoon/components/accessor/Accessor.java cocoon/blocks/unsupported/template/trunk/java/org/apache/cocoon/components/accessor/ContextAccessor.java cocoon/blocks/unsupported/template/trunk/java/org/apache/cocoon/components/accessor/ContinuationAccessor.java cocoon/blocks/unsupported/template/trunk/java/org/apache/cocoon/components/accessor/FlowAttributeAccessor.java cocoon/blocks/unsupported/template/trunk/java/org/apache/cocoon/components/accessor/MapAccessor.java cocoon/blocks/unsupported/template/trunk/java/org/apache/cocoon/components/accessor/ObjectModelAccessor.java cocoon/blocks/unsupported/template/trunk/java/org/apache/cocoon/components/accessor/RequestAccessor.java cocoon/blocks/unsupported/template/trunk/java/org/apache/cocoon/components/accessor/SessionAccessor.java cocoon/blocks/unsupported/template/trunk/java/org/apache/cocoon/components/accessor/accessor.roles cocoon/blocks/unsupported/template/trunk/test/org/apache/cocoon/components/accessor/ cocoon/blocks/unsupported/template/trunk/test/org/apache/cocoon/components/accessor/AccessorTestCase.java cocoon/blocks/unsupported/template/trunk/test/org/apache/cocoon/components/accessor/AccessorTestCase.xtest Added: cocoon/blocks/unsupported/template/trunk/java/org/apache/cocoon/components/accessor/Accessor.java URL: http://svn.apache.org/viewcvs/cocoon/blocks/unsupported/template/trunk/java/org/apache/cocoon/components/accessor/Accessor.java?view=auto&rev=158239 ============================================================================== --- cocoon/blocks/unsupported/template/trunk/java/org/apache/cocoon/components/accessor/Accessor.java (added) +++ cocoon/blocks/unsupported/template/trunk/java/org/apache/cocoon/components/accessor/Accessor.java Sat Mar 19 06:00:44 2005 @@ -0,0 +1,22 @@ +/* + * Copyright 1999-2004 The Apache Software Foundation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.cocoon.components.accessor; + +public interface Accessor { + public String ROLE = Accessor.class.getName(); + + public Object getObject(); +} Added: cocoon/blocks/unsupported/template/trunk/java/org/apache/cocoon/components/accessor/ContextAccessor.java URL: http://svn.apache.org/viewcvs/cocoon/blocks/unsupported/template/trunk/java/org/apache/cocoon/components/accessor/ContextAccessor.java?view=auto&rev=158239 ============================================================================== --- cocoon/blocks/unsupported/template/trunk/java/org/apache/cocoon/components/accessor/ContextAccessor.java (added) +++ cocoon/blocks/unsupported/template/trunk/java/org/apache/cocoon/components/accessor/ContextAccessor.java Sat Mar 19 06:00:44 2005 @@ -0,0 +1,25 @@ +/* + * Copyright 1999-2004 The Apache Software Foundation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.cocoon.components.accessor; + +import org.apache.cocoon.environment.ObjectModelHelper; + +public class ContextAccessor extends ObjectModelAccessor { + + public Object getObject() { + return ObjectModelHelper.getContext(getObjectModel()); + } +} Added: cocoon/blocks/unsupported/template/trunk/java/org/apache/cocoon/components/accessor/ContinuationAccessor.java URL: http://svn.apache.org/viewcvs/cocoon/blocks/unsupported/template/trunk/java/org/apache/cocoon/components/accessor/ContinuationAccessor.java?view=auto&rev=158239 ============================================================================== --- cocoon/blocks/unsupported/template/trunk/java/org/apache/cocoon/components/accessor/ContinuationAccessor.java (added) +++ cocoon/blocks/unsupported/template/trunk/java/org/apache/cocoon/components/accessor/ContinuationAccessor.java Sat Mar 19 06:00:44 2005 @@ -0,0 +1,25 @@ +/* + * Copyright 1999-2004 The Apache Software Foundation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.cocoon.components.accessor; + +import org.apache.cocoon.components.flow.FlowHelper; + +public class ContinuationAccessor extends ObjectModelAccessor { + + public Object getObject() { + return FlowHelper.getWebContinuation(getObjectModel()); + } +} Added: cocoon/blocks/unsupported/template/trunk/java/org/apache/cocoon/components/accessor/FlowAttributeAccessor.java URL: http://svn.apache.org/viewcvs/cocoon/blocks/unsupported/template/trunk/java/org/apache/cocoon/components/accessor/FlowAttributeAccessor.java?view=auto&rev=158239 ============================================================================== --- cocoon/blocks/unsupported/template/trunk/java/org/apache/cocoon/components/accessor/FlowAttributeAccessor.java (added) +++ cocoon/blocks/unsupported/template/trunk/java/org/apache/cocoon/components/accessor/FlowAttributeAccessor.java Sat Mar 19 06:00:44 2005 @@ -0,0 +1,38 @@ +/* + * Copyright 1999-2004 The Apache Software Foundation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.cocoon.components.accessor; + +import java.util.HashMap; +import java.util.Map; + +import org.apache.cocoon.components.flow.FlowHelper; +import org.apache.cocoon.environment.TemplateObjectModelHelper; + +public class FlowAttributeAccessor extends ObjectModelAccessor { + + public Object getObject() { + Map map = new HashMap(); + Object contextObject = FlowHelper.getContextObject(getObjectModel()); + + if (contextObject instanceof Map) { + map.putAll((Map)contextObject); + } else if (contextObject != null) { + TemplateObjectModelHelper.fillContext(contextObject, map); + } + + return map; + } +} Added: cocoon/blocks/unsupported/template/trunk/java/org/apache/cocoon/components/accessor/MapAccessor.java URL: http://svn.apache.org/viewcvs/cocoon/blocks/unsupported/template/trunk/java/org/apache/cocoon/components/accessor/MapAccessor.java?view=auto&rev=158239 ============================================================================== --- cocoon/blocks/unsupported/template/trunk/java/org/apache/cocoon/components/accessor/MapAccessor.java (added) +++ cocoon/blocks/unsupported/template/trunk/java/org/apache/cocoon/components/accessor/MapAccessor.java Sat Mar 19 06:00:44 2005 @@ -0,0 +1,130 @@ +/* + * Copyright 1999-2004 The Apache Software Foundation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.cocoon.components.accessor; + +import java.util.HashMap; +import java.util.Map; + +import org.apache.avalon.framework.logger.AbstractLogEnabled; +import org.apache.avalon.framework.configuration.Configurable; +import org.apache.avalon.framework.configuration.Configuration; +import org.apache.avalon.framework.configuration.ConfigurationException; +import org.apache.avalon.framework.activity.Disposable; +import org.apache.avalon.framework.service.ServiceException; +import org.apache.avalon.framework.service.ServiceManager; +import org.apache.avalon.framework.service.Serviceable; +import org.apache.avalon.framework.service.ServiceSelector; +import org.apache.avalon.framework.thread.ThreadSafe; + +/** + * An accessor that handles a <code>Map</code> of accessors. + */ +public class MapAccessor implements Accessor, Configurable, Serviceable, ThreadSafe { + + /** Reference to the accessors */ + private Map accessors = null; + + /** The service manager instance */ + private ServiceManager manager; + + /** + * The accessors that should be part of the map are configured + * through <code><element name="the key that will be used in + * the map" accessor="the name of the accessor"/></code>. + */ + public void configure(Configuration config) throws ConfigurationException { + HashMap accessors = new HashMap(); + Configuration[] children = config.getChildren("element"); + for (int i = 0; i < children.length; i++) { + String accessor = children[i].getAttribute("accessor"); + // use the accessor name as name if nothing else is given + String name = children[i].getAttribute("name", accessor); + accessors.put(name, accessor); + } + this.accessors = new AccessorMap(accessors, this.manager); + } + + /** + * Set the current <code>ServiceManager</code> instance used by this + * <code>Serviceable</code>. + */ + public void service(ServiceManager manager) throws ServiceException { + this.manager = manager; + } + + /** + * Release all resources. + */ + public void dispose() { + this.manager = null; + } + + /** + * Get the map of accessors + */ + public Object getObject() { + return this.accessors; + } + + /** + * Map that finds the accessor at access time. + */ + public class AccessorMap extends HashMap { + + private ServiceManager manager; + + public AccessorMap(Map map, ServiceManager manager) { + super(map); + this.manager = manager; + } + + public Object get(Object key) { + String accessorName = (String)super.get(key); + if (accessorName == null) + return null; + ServiceSelector accessorSelector = null; + Accessor accessor = null; + try { + // Thread safe accessors could be looked up once and be cached + accessorSelector = + (ServiceSelector)this.manager.lookup(Accessor.ROLE + "Selector"); + accessor = (Accessor)accessorSelector.select(accessorName); + return accessor.getObject(); + } catch (ServiceException e) { + // FIXME: Don't know if this is the appropriate action + throw new RuntimeException("Trying to access non existing acessor: " + + accessorName, e); + } finally { + accessorSelector.release(accessor); + this.manager.release(accessorSelector); + } + } + + // The MapAccessor is thread safe so the map should be read only + public void clear() { + throw new UnsupportedOperationException("AccessorMap is read only"); + } + public Object put(Object key, Object value) { + throw new UnsupportedOperationException("AccessorMap is read only"); + } + public void putAll(Map m) { + throw new UnsupportedOperationException("AccessorMap is read only"); + } + public Object remove(Object key) { + throw new UnsupportedOperationException("AccessorMap is read only"); + } + } +} Added: cocoon/blocks/unsupported/template/trunk/java/org/apache/cocoon/components/accessor/ObjectModelAccessor.java URL: http://svn.apache.org/viewcvs/cocoon/blocks/unsupported/template/trunk/java/org/apache/cocoon/components/accessor/ObjectModelAccessor.java?view=auto&rev=158239 ============================================================================== --- cocoon/blocks/unsupported/template/trunk/java/org/apache/cocoon/components/accessor/ObjectModelAccessor.java (added) +++ cocoon/blocks/unsupported/template/trunk/java/org/apache/cocoon/components/accessor/ObjectModelAccessor.java Sat Mar 19 06:00:44 2005 @@ -0,0 +1,41 @@ +/* + * Copyright 1999-2004 The Apache Software Foundation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.cocoon.components.accessor; + +import java.util.Map; + +import org.apache.avalon.framework.context.Context; +import org.apache.avalon.framework.context.ContextException; +import org.apache.avalon.framework.context.Contextualizable; +import org.apache.cocoon.components.ContextHelper; + +public class ObjectModelAccessor implements Accessor, Contextualizable { + + private Context context; + + public void contextualize(Context context) + throws ContextException{ + this.context = context; + } + + protected Map getObjectModel() { + return ContextHelper.getObjectModel(this.context); + } + + public Object getObject() { + return getObjectModel(); + } +} Added: cocoon/blocks/unsupported/template/trunk/java/org/apache/cocoon/components/accessor/RequestAccessor.java URL: http://svn.apache.org/viewcvs/cocoon/blocks/unsupported/template/trunk/java/org/apache/cocoon/components/accessor/RequestAccessor.java?view=auto&rev=158239 ============================================================================== --- cocoon/blocks/unsupported/template/trunk/java/org/apache/cocoon/components/accessor/RequestAccessor.java (added) +++ cocoon/blocks/unsupported/template/trunk/java/org/apache/cocoon/components/accessor/RequestAccessor.java Sat Mar 19 06:00:44 2005 @@ -0,0 +1,25 @@ +/* + * Copyright 1999-2004 The Apache Software Foundation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.cocoon.components.accessor; + +import org.apache.cocoon.environment.ObjectModelHelper; + +public class RequestAccessor extends ObjectModelAccessor { + + public Object getObject() { + return ObjectModelHelper.getRequest(getObjectModel()); + } +} Added: cocoon/blocks/unsupported/template/trunk/java/org/apache/cocoon/components/accessor/SessionAccessor.java URL: http://svn.apache.org/viewcvs/cocoon/blocks/unsupported/template/trunk/java/org/apache/cocoon/components/accessor/SessionAccessor.java?view=auto&rev=158239 ============================================================================== --- cocoon/blocks/unsupported/template/trunk/java/org/apache/cocoon/components/accessor/SessionAccessor.java (added) +++ cocoon/blocks/unsupported/template/trunk/java/org/apache/cocoon/components/accessor/SessionAccessor.java Sat Mar 19 06:00:44 2005 @@ -0,0 +1,29 @@ +/* + * Copyright 1999-2004 The Apache Software Foundation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.cocoon.components.accessor; + +import org.apache.cocoon.environment.ObjectModelHelper; +import org.apache.cocoon.environment.Request; +import org.apache.cocoon.environment.Session; + +public class SessionAccessor extends ObjectModelAccessor { + + public Object getObject() { + Request request = ObjectModelHelper.getRequest(getObjectModel()); + Session session = request.getSession(false); + return session; + } +} Added: cocoon/blocks/unsupported/template/trunk/java/org/apache/cocoon/components/accessor/accessor.roles URL: http://svn.apache.org/viewcvs/cocoon/blocks/unsupported/template/trunk/java/org/apache/cocoon/components/accessor/accessor.roles?view=auto&rev=158239 ============================================================================== --- cocoon/blocks/unsupported/template/trunk/java/org/apache/cocoon/components/accessor/accessor.roles (added) +++ cocoon/blocks/unsupported/template/trunk/java/org/apache/cocoon/components/accessor/accessor.roles Sat Mar 19 06:00:44 2005 @@ -0,0 +1,22 @@ +<?xml version="1.0"?> +<!-- + Copyright 1999-2004 The Apache Software Foundation + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +--> +<!-- $Id$ --> +<role-list> + <role name="org.apache.cocoon.components.accessor.AccessorSelector" + shorthand="accessors" + default-class="org.apache.cocoon.core.container.DefaultServiceSelector"/> +</role-list> \ No newline at end of file Added: cocoon/blocks/unsupported/template/trunk/test/org/apache/cocoon/components/accessor/AccessorTestCase.java URL: http://svn.apache.org/viewcvs/cocoon/blocks/unsupported/template/trunk/test/org/apache/cocoon/components/accessor/AccessorTestCase.java?view=auto&rev=158239 ============================================================================== --- cocoon/blocks/unsupported/template/trunk/test/org/apache/cocoon/components/accessor/AccessorTestCase.java (added) +++ cocoon/blocks/unsupported/template/trunk/test/org/apache/cocoon/components/accessor/AccessorTestCase.java Sat Mar 19 06:00:44 2005 @@ -0,0 +1,84 @@ +/* + * Copyright 1999-2004 The Apache Software Foundation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.cocoon.components.accessor; + +import java.util.Map; + +import org.apache.avalon.framework.logger.ConsoleLogger; +import org.apache.avalon.framework.logger.Logger; +import org.apache.avalon.framework.service.ServiceException; +import org.apache.avalon.framework.service.ServiceSelector; +import org.apache.cocoon.SitemapComponentTestCase; +import org.apache.cocoon.components.accessor.Accessor; +import org.apache.cocoon.environment.Context; +import org.apache.cocoon.environment.Request; +import org.apache.cocoon.environment.Session; + +public class AccessorTestCase extends SitemapComponentTestCase { + private Logger logger = new ConsoleLogger(ConsoleLogger.LEVEL_WARN); + + protected Logger getLogger() { + return this.logger; + } + + public void testRequestAccessor() throws ServiceException { + ServiceSelector accessorSelector = + (ServiceSelector)this.lookup(Accessor.ROLE + "Selector"); + Accessor accessor = (Accessor)accessorSelector.select("request"); + Request request = (Request)accessor.getObject(); + assertEquals("HTTP/1.1", request.getProtocol()); + accessorSelector.release(accessor); + this.release(accessorSelector); + } + + public void testSessionAccessor() throws ServiceException { + // Create a session + getRequest().getSession(); + ServiceSelector accessorSelector = + (ServiceSelector)this.lookup(Accessor.ROLE + "Selector"); + Accessor accessor = (Accessor)accessorSelector.select("session"); + Session session = (Session)accessor.getObject(); + assertEquals("MockSession", session.getId()); + accessorSelector.release(accessor); + this.release(accessorSelector); + } + + public void testContextAccessor() throws ServiceException { + getContext().setAttribute("foo", "bar"); + ServiceSelector accessorSelector = + (ServiceSelector)this.lookup(Accessor.ROLE + "Selector"); + Accessor accessor = (Accessor)accessorSelector.select("context"); + Context context = (Context)accessor.getObject(); + assertEquals("bar", context.getAttribute("foo")); + accessorSelector.release(accessor); + this.release(accessorSelector); + } + + public void testMapAccessor() throws ServiceException { + // Create a session + getRequest().getSession(); + getContext().setAttribute("foo", "bar"); + ServiceSelector accessorSelector = + (ServiceSelector)this.lookup(Accessor.ROLE + "Selector"); + Accessor accessor = (Accessor)accessorSelector.select("cocoon"); + Map map = (Map)accessor.getObject(); + assertEquals("HTTP/1.1", ((Request)map.get("request")).getProtocol()); + assertEquals("MockSession", ((Session)map.get("session")).getId()); + assertEquals("bar", ((Context)map.get("context")).getAttribute("foo")); + accessorSelector.release(accessor); + this.release(accessorSelector); + } +} Added: cocoon/blocks/unsupported/template/trunk/test/org/apache/cocoon/components/accessor/AccessorTestCase.xtest URL: http://svn.apache.org/viewcvs/cocoon/blocks/unsupported/template/trunk/test/org/apache/cocoon/components/accessor/AccessorTestCase.xtest?view=auto&rev=158239 ============================================================================== --- cocoon/blocks/unsupported/template/trunk/test/org/apache/cocoon/components/accessor/AccessorTestCase.xtest (added) +++ cocoon/blocks/unsupported/template/trunk/test/org/apache/cocoon/components/accessor/AccessorTestCase.xtest Sat Mar 19 06:00:44 2005 @@ -0,0 +1,42 @@ +<?xml version="1.0"?> +<!-- + Copyright 1999-2004 The Apache Software Foundation + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +--> + +<testcase> + <roles> + <role name="org.apache.cocoon.components.accessor.AccessorSelector" + shorthand="accessors" + default-class="org.apache.cocoon.core.container.DefaultServiceSelector"/> + </roles> + + <components> + <accessors> + <component-instance class="org.apache.cocoon.components.accessor.RequestAccessor" name="request"/> + <component-instance class="org.apache.cocoon.components.accessor.SessionAccessor" name="session"/> + <component-instance class="org.apache.cocoon.components.accessor.ContextAccessor" name="context"/> + <component-instance class="org.apache.cocoon.components.accessor.ContinuationAccessor" name="continuation"/> + <component-instance class="org.apache.cocoon.components.accessor.FlowAttributeAccessor" name="flow-attribute"/> + <component-instance class="org.apache.cocoon.components.accessor.MapAccessor" name="cocoon"> + <element accessor="request"/> + <element accessor="session"/> + <element accessor="context"/> + <element accessor="continuation"/> + <element name="flowcontext" accessor="flow-attribute"/> + </component-instance> + </accessors> + </components> + +</testcase>