001: /*
002: * This file is part of the WfMOpen project.
003: * Copyright (C) 2001-2003 Danet GmbH (www.danet.de), GS-AN.
004: * All rights reserved.
005: *
006: * This program is free software; you can redistribute it and/or modify
007: * it under the terms of the GNU General Public License as published by
008: * the Free Software Foundation; either version 2 of the License, or
009: * (at your option) any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: * GNU General Public License for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * along with this program; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: *
020: * $Id: WorkflowServiceConnection.java,v 1.6 2006/12/12 14:22:08 drmlipp Exp $
021: *
022: * $Log: WorkflowServiceConnection.java,v $
023: * Revision 1.6 2006/12/12 14:22:08 drmlipp
024: * Merged XForms client from branch.
025: *
026: * Revision 1.5.2.1 2006/12/05 14:18:42 drmlipp
027: * Initial display working.
028: *
029: * Revision 1.5 2006/10/22 20:13:30 mlipp
030: * Added preferences for workflow service.
031: *
032: * Revision 1.4 2006/09/29 12:32:12 drmlipp
033: * Consistently using WfMOpen as projct name now.
034: *
035: * Revision 1.3 2006/09/27 12:49:14 drmlipp
036: * Removed specific configurability (done using JNDI environement entries).
037: *
038: * Revision 1.2 2006/09/26 16:12:45 drmlipp
039: * Made JNDI configurable.
040: *
041: * Revision 1.1 2006/09/21 11:52:00 drmlipp
042: * Added editor.
043: *
044: * Revision 1.3 2005/11/07 14:36:11 drmlipp
045: * Adapted to revised request attribute handling.
046: *
047: * Revision 1.2 2005/10/14 12:45:35 drmlipp
048: * Added information.
049: *
050: * Revision 1.1 2005/09/28 15:11:00 drmlipp
051: * Optimized and simplified.
052: *
053: * Revision 1.4 2005/09/16 15:06:51 drmlipp
054: * Fixed typo.
055: *
056: * Revision 1.3 2005/06/22 15:14:27 drmlipp
057: * Improved lifecycle handling.
058: *
059: * Revision 1.2 2005/05/11 15:17:40 drmlipp
060: * Changed scope to session.
061: *
062: * Revision 1.1 2005/05/11 14:28:07 drmlipp
063: * Started JSF utility package.
064: *
065: */
066: package de.danet.an.xformstool.portletapp;
067:
068: import java.io.Serializable;
069: import java.util.Hashtable;
070:
071: import javax.faces.context.FacesContext;
072: import javax.faces.el.EvaluationException;
073: import javax.naming.Context;
074: import javax.portlet.PortletRequest;
075:
076: import de.danet.an.util.ResourceNotAvailableException;
077: import de.danet.an.workflow.api.FactoryConfigurationError;
078: import de.danet.an.workflow.api.WorkflowService;
079: import de.danet.an.workflow.api.WorkflowServiceFactory;
080:
081: /**
082: * This class implements a managed bean that holds the connection to the
083: * workflow service.
084: */
085: public class WorkflowServiceConnection implements Serializable {
086:
087: private static final org.apache.commons.logging.Log logger = org.apache.commons.logging.LogFactory
088: .getLog(WorkflowServiceConnection.class);
089:
090: private transient WorkflowService wfs = null;
091:
092: /**
093: * Create a new instance initializing all attributes to default values.
094: */
095: public WorkflowServiceConnection() {
096: if (logger.isDebugEnabled()) {
097: logger.debug("Created managed bean");
098: }
099: }
100:
101: /**
102: * Utility function to get the workflow engine.
103: * @return the workflow engine stub.
104: * @throws ResourceNotAvailableException if the engine is not available.
105: */
106: public WorkflowService getWorkflowService()
107: throws EvaluationException {
108: try {
109: if (wfs == null) {
110: if (logger.isDebugEnabled()) {
111: logger
112: .debug("Creating connection to workflow service");
113: }
114: // Get workflow service
115: WorkflowServiceFactory wfsf = WorkflowServiceFactory
116: .newInstance();
117: Object req = FacesContext.getCurrentInstance()
118: .getExternalContext().getRequest();
119: if (req instanceof PortletRequest) {
120: String wejn = ((PortletRequest) req)
121: .getPreferences().getValue(
122: "workflowEngineJndiName", "");
123: if (wejn != null && wejn.length() > 0) {
124: wfsf.setProperty("de.danet.an.workflow.engine",
125: wejn);
126: }
127: String icf = ((PortletRequest) req)
128: .getPreferences().getValue(
129: "initialContextFactory", "");
130: String icu = ((PortletRequest) req)
131: .getPreferences().getValue(
132: "initialContextUrl", "");
133: if (icf != null && icf.length() > 0 || icu != null
134: && icu.length() > 0) {
135: Hashtable env = new Hashtable();
136: if (icf != null && icf.length() > 0) {
137: env.put(Context.INITIAL_CONTEXT_FACTORY,
138: icf);
139: }
140: if (icu != null && icu.length() > 0) {
141: env.put(Context.PROVIDER_URL, icu);
142: }
143: wfsf
144: .setProperty(
145: "javax.naming.InitialContext.Environment",
146: env);
147: }
148: }
149: wfs = wfsf.newWorkflowService();
150: }
151: return wfs;
152: } catch (FactoryConfigurationError e) {
153: logger.error(e.getMessage(), e);
154: throw new EvaluationException(e.getMessage());
155: }
156: }
157:
158: /**
159: * Convenience method to get an instance of this bean.
160: */
161: public static WorkflowServiceConnection instance(String beanName)
162: throws EvaluationException {
163: FacesContext fc = FacesContext.getCurrentInstance();
164: return (WorkflowServiceConnection) fc.getApplication()
165: .getVariableResolver().resolveVariable(fc, beanName);
166: }
167: }
|