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.workflow.impl;
019:
020: import java.util.HashMap;
021: import java.util.Map;
022:
023: import org.apache.avalon.excalibur.pool.Poolable;
024: import org.apache.avalon.framework.logger.AbstractLogEnabled;
025: import org.apache.avalon.framework.service.ServiceException;
026: import org.apache.avalon.framework.service.ServiceManager;
027: import org.apache.avalon.framework.service.Serviceable;
028: import org.apache.lenya.cms.cocoon.source.SourceUtil;
029: import org.apache.lenya.workflow.Workflow;
030: import org.apache.lenya.workflow.WorkflowEngine;
031: import org.apache.lenya.workflow.WorkflowException;
032: import org.apache.lenya.workflow.WorkflowManager;
033: import org.apache.lenya.workflow.Workflowable;
034: import org.w3c.dom.Document;
035:
036: /**
037: * Workflow manager implementation.
038: *
039: * @version $Id: WorkflowManagerImpl.java 179751 2005-06-03 09:13:35Z andreas $
040: */
041: public class WorkflowManagerImpl extends AbstractLogEnabled implements
042: WorkflowManager, Serviceable, Poolable {
043:
044: private Map uri2workflow = new HashMap();
045:
046: /**
047: * @see org.apache.lenya.workflow.WorkflowManager#invoke(org.apache.lenya.workflow.Workflowable,
048: * java.lang.String, boolean)
049: */
050: public void invoke(Workflowable workflowable, String event,
051: boolean force) throws WorkflowException {
052: if (hasWorkflow(workflowable)) {
053: WorkflowEngine engine = new WorkflowEngineImpl();
054: Workflow workflow = getWorkflowSchema(workflowable);
055:
056: if (force
057: && !engine.canInvoke(workflowable, workflow, event)) {
058: throw new WorkflowException("The event [" + event
059: + "] cannot be invoked on the document ["
060: + workflowable + "]");
061: }
062: engine.invoke(workflowable, workflow, event);
063: }
064: }
065:
066: /**
067: * @see org.apache.lenya.workflow.WorkflowManager#invoke(org.apache.lenya.workflow.Workflowable,
068: * java.lang.String)
069: */
070: public void invoke(Workflowable workflowable, String event)
071: throws WorkflowException {
072: invoke(workflowable, event, true);
073: }
074:
075: /**
076: * @see org.apache.lenya.workflow.WorkflowManager#canInvoke(org.apache.lenya.workflow.Workflowable,
077: * java.lang.String)
078: */
079: public boolean canInvoke(Workflowable workflowable, String event) {
080: boolean canInvoke = true;
081: try {
082: if (hasWorkflow(workflowable)) {
083: Workflow workflow = getWorkflowSchema(workflowable);
084: WorkflowEngine engine = new WorkflowEngineImpl();
085: canInvoke = engine.canInvoke(workflowable, workflow,
086: event);
087: }
088: } catch (Exception e) {
089: throw new RuntimeException(e);
090: }
091: return canInvoke;
092: }
093:
094: protected ServiceManager manager;
095:
096: /**
097: * @see org.apache.avalon.framework.service.Serviceable#service(org.apache.avalon.framework.service.ServiceManager)
098: */
099: public void service(ServiceManager manager) throws ServiceException {
100: this .manager = manager;
101: }
102:
103: /**
104: * @see org.apache.lenya.workflow.WorkflowManager#getWorkflowSchema(org.apache.lenya.workflow.Workflowable)
105: */
106: public Workflow getWorkflowSchema(Workflowable workflowable)
107: throws WorkflowException {
108: WorkflowImpl workflow = null;
109:
110: try {
111: String uri = workflowable.getWorkflowSchemaURI();
112: getLogger().debug("Workflow URI: " + uri);
113: if (uri != null) {
114: workflow = (WorkflowImpl) this .uri2workflow.get(uri);
115: if (workflow == null) {
116: Document document = SourceUtil.readDOM(uri,
117: this .manager);
118: if (document == null) {
119: throw new WorkflowException(
120: "Could not read workflow schema from URI ["
121: + uri + "]!");
122: }
123: WorkflowBuilder builder = new WorkflowBuilder(
124: getLogger());
125: workflow = builder.buildWorkflow(uri, document);
126: this .uri2workflow.put(uri, workflow);
127: }
128: }
129: } catch (final Exception e) {
130: throw new WorkflowException(e);
131: }
132:
133: return workflow;
134: }
135:
136: /**
137: * @see org.apache.lenya.workflow.WorkflowManager#hasWorkflow(org.apache.lenya.workflow.Workflowable)
138: */
139: public boolean hasWorkflow(Workflowable workflowable) {
140: return workflowable.getWorkflowSchemaURI() != null;
141: }
142:
143: }
|