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: */
018: package org.apache.lenya.cms.usecase.impl;
019:
020: import java.util.ArrayList;
021: import java.util.Collections;
022: import java.util.Iterator;
023: import java.util.List;
024: import java.util.Map;
025:
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.lenya.cms.repository.Session;
031: import org.apache.lenya.cms.usecase.Usecase;
032: import org.apache.lenya.cms.usecase.UsecaseException;
033: import org.apache.lenya.cms.usecase.UsecaseInvoker;
034: import org.apache.lenya.cms.usecase.UsecaseMessage;
035: import org.apache.lenya.cms.usecase.UsecaseResolver;
036:
037: /**
038: * Usecase invoker implementation.
039: *
040: * @version $Id: UsecaseInvokerImpl.java 556887 2007-07-17 11:31:09Z andreas $
041: */
042: public class UsecaseInvokerImpl extends AbstractLogEnabled implements
043: UsecaseInvoker, Serviceable {
044:
045: private String targetUrl;
046:
047: /**
048: * @see org.apache.lenya.cms.usecase.UsecaseInvoker#invoke(java.lang.String, java.lang.String,
049: * java.util.Map)
050: */
051: public void invoke(String webappUrl, String usecaseName,
052: Map parameters) throws UsecaseException {
053:
054: this .errorMessages.clear();
055: this .infoMessages.clear();
056:
057: UsecaseResolver resolver = null;
058: Usecase usecase = null;
059: this .result = SUCCESS;
060: try {
061:
062: resolver = (UsecaseResolver) this .manager
063: .lookup(UsecaseResolver.ROLE);
064: usecase = resolver.resolve(webappUrl, usecaseName);
065:
066: Session testSession = getTestSession();
067: if (testSession != null) {
068: usecase.setTestSession(testSession);
069: }
070:
071: passParameters(usecase, parameters);
072:
073: usecase.checkPreconditions();
074:
075: if (succeeded(PRECONDITIONS_FAILED, usecase)) {
076:
077: usecase.lockInvolvedObjects();
078: usecase.checkExecutionConditions();
079:
080: if (succeeded(EXECUTION_CONDITIONS_FAILED, usecase)) {
081: usecase.execute();
082:
083: boolean success = succeeded(EXECUTION_FAILED,
084: usecase);
085: this .targetUrl = usecase.getTargetURL(success);
086: if (success) {
087: usecase.checkPostconditions();
088: succeeded(POSTCONDITIONS_FAILED, usecase);
089: }
090: }
091: }
092:
093: } catch (Exception e) {
094: throw new RuntimeException(e);
095: } finally {
096: if (resolver != null) {
097: if (usecase != null) {
098: try {
099: resolver.release(usecase);
100: } catch (ServiceException e) {
101: throw new RuntimeException(e);
102: }
103: }
104: this .manager.release(resolver);
105: }
106: }
107: }
108:
109: private Session testSession = null;
110:
111: protected Session getTestSession() {
112: return this .testSession;
113: }
114:
115: protected boolean succeeded(int result, Usecase usecase) {
116:
117: this .errorMessages.addAll(usecase.getErrorMessages());
118: this .infoMessages.addAll(usecase.getInfoMessages());
119:
120: if (usecase.getErrorMessages().isEmpty()) {
121: return true;
122: } else {
123: this .result = result;
124: String message = null;
125: switch (result) {
126: case PRECONDITIONS_FAILED:
127: message = "Precondition messages:";
128: break;
129: case EXECUTION_CONDITIONS_FAILED:
130: message = "Execution condition messages:";
131: break;
132: case EXECUTION_FAILED:
133: message = "Execution messages:";
134: break;
135: case POSTCONDITIONS_FAILED:
136: message = "Postcondition messages:";
137: break;
138: }
139: logErrorMessages(usecase.getName(), message, usecase
140: .getErrorMessages());
141: return false;
142: }
143: }
144:
145: /**
146: * @param usecase The usecase to pass the parameters to.
147: * @param parameters The parameters.
148: */
149: protected void passParameters(Usecase usecase, Map parameters) {
150: for (Iterator i = parameters.keySet().iterator(); i.hasNext();) {
151: String key = (String) i.next();
152: Object value = parameters.get(key);
153: usecase.setParameter(key, value);
154: }
155: }
156:
157: /**
158: * @param usecaseName The name of the usecase.
159: * @param headline The headline of the messages.
160: * @param errorMessages The messages to log.
161: */
162: protected void logErrorMessages(String usecaseName,
163: String headline, List errorMessages) {
164: getLogger()
165: .error("Usecase [" + usecaseName + "] - " + headline);
166: for (Iterator i = errorMessages.iterator(); i.hasNext();) {
167: getLogger().error("" + (UsecaseMessage) i.next());
168: }
169: }
170:
171: private ServiceManager manager;
172:
173: /**
174: * @see org.apache.avalon.framework.service.Serviceable#service(org.apache.avalon.framework.service.ServiceManager)
175: */
176: public void service(ServiceManager manager) throws ServiceException {
177: this .manager = manager;
178: }
179:
180: private List errorMessages = new ArrayList();
181: private List infoMessages = new ArrayList();
182:
183: public List getErrorMessages() {
184: return Collections.unmodifiableList(this .errorMessages);
185: }
186:
187: public List getInfoMessages() {
188: return Collections.unmodifiableList(this .infoMessages);
189: }
190:
191: private int result = SUCCESS;
192:
193: public int getResult() {
194: return this .result;
195: }
196:
197: public String getTargetUrl() {
198: if (this .targetUrl == null) {
199: throw new IllegalStateException(
200: "The usecase has not been executed yet.");
201: }
202: return this .targetUrl;
203: }
204:
205: public void setTestSession(Session session) {
206: this.testSession = session;
207: }
208:
209: }
|