001: /******************************************************************************
002: * JBoss, a division of Red Hat *
003: * Copyright 2006, Red Hat Middleware, LLC, and individual *
004: * contributors as indicated by the @authors tag. See the *
005: * copyright.txt in the distribution for a full listing of *
006: * individual contributors. *
007: * *
008: * This is free software; you can redistribute it and/or modify it *
009: * under the terms of the GNU Lesser General Public License as *
010: * published by the Free Software Foundation; either version 2.1 of *
011: * the License, or (at your option) any later version. *
012: * *
013: * This software is distributed in the hope that it will be useful, *
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of *
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
016: * Lesser General Public License for more details. *
017: * *
018: * You should have received a copy of the GNU Lesser General Public *
019: * License along with this software; if not, write to the Free *
020: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA *
021: * 02110-1301 USA, or see the FSF site: http://www.fsf.org. *
022: ******************************************************************************/package org.jboss.portal.core.controller;
023:
024: import org.jboss.logging.Logger;
025: import org.jboss.portal.common.invocation.Invocation;
026: import org.jboss.portal.common.invocation.InvocationContext;
027: import org.jboss.portal.common.invocation.InvocationException;
028: import org.jboss.portal.common.invocation.InvocationHandler;
029: import org.jboss.portal.common.invocation.Scope;
030: import org.jboss.portal.core.controller.command.info.CommandInfo;
031: import org.jboss.portal.security.PortalSecurityException;
032: import org.jboss.portal.security.spi.auth.PortalAuthorizationManager;
033:
034: /**
035: * A controller command.
036: *
037: * @author <a href="mailto:julien@jboss.org">Julien Viet</a>
038: * @version $Revision: 8786 $
039: */
040: public abstract class ControllerCommand extends Invocation {
041:
042: /** . */
043: public static final Scope PRINCIPAL_SCOPE = Scope.PRINCIPAL_SCOPE;
044:
045: /** . */
046: public static final Scope SESSION_SCOPE = Scope.SESSION_SCOPE;
047:
048: /** . */
049: public static final Scope REQUEST_SCOPE = Scope.REQUEST_SCOPE;
050:
051: /** . */
052: public static final Scope NAVIGATIONAL_STATE_SCOPE = new Scope(
053: "navigationalstate");
054:
055: /** . */
056: protected static Logger log = Logger
057: .getLogger(ControllerCommand.class);
058:
059: /** The context of the command. */
060: protected ControllerContext context;
061:
062: /** Execute command when the end of the stack is reached. */
063: private static final InvocationHandler handler = new InvocationHandler() {
064: public Object invoke(Invocation invocation) throws Exception,
065: InvocationException {
066: ControllerCommand cmd = (ControllerCommand) invocation;
067: return cmd.execute();
068: }
069: };
070:
071: protected ControllerCommand() {
072: setHandler(handler);
073: }
074:
075: /** Return the meta data of this command. */
076: public abstract CommandInfo getInfo();
077:
078: public final InvocationContext getContext() {
079: if (context == null) {
080: throw new IllegalStateException();
081: }
082: return context;
083: }
084:
085: public final ControllerContext getControllerContext() {
086: return context;
087: }
088:
089: /**
090: * Enforce the security on this command.
091: *
092: * @throws PortalSecurityException
093: * @throws org.jboss.portal.core.controller.SecurityException
094: *
095: */
096: public void enforceSecurity(PortalAuthorizationManager pam)
097: throws SecurityException {
098: }
099:
100: public void acquireResources() throws NoSuchResourceException {
101: }
102:
103: public void releaseResources() {
104: }
105:
106: /** Contextualize the command. */
107: public final void createContext(ControllerContext context)
108: throws ControllerException {
109: this .context = context;
110:
111: //
112: create();
113: }
114:
115: /** Destroy state after invocation. */
116: public final void destroyContext() {
117: try {
118: destroy();
119: } finally {
120: this .context = null;
121: }
122: }
123:
124: protected void create() {
125: }
126:
127: protected void destroy() {
128: }
129:
130: /** Execute the command. */
131: public abstract ControllerResponse execute()
132: throws ControllerException;
133:
134: public static void rethrow(Exception e) throws ControllerException,
135: InvocationException, RuntimeException {
136: if (e instanceof InvocationException) {
137: throw (InvocationException) e;
138: }
139: if (e instanceof ControllerException) {
140: throw (ControllerException) e;
141: }
142: if (e instanceof RuntimeException) {
143: throw (RuntimeException) e;
144: }
145: throw new ControllerException(e);
146: }
147: }
|