ludc
2025-01-16 986aa62ed00bee39363bab41b4eeb8259d446efd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
/*
 * JBoss, Home of Professional Open Source
 * Copyright 2005, JBoss Inc., and individual contributors as indicated
 * by the @authors tag. See the copyright.txt in the distribution for a
 * full listing of individual contributors.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package org.jbpm.pvm.internal.processengine;
 
import java.io.Serializable;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
 
import javax.naming.InitialContext;
import javax.naming.NamingException;
 
import org.jbpm.api.ExecutionService;
import org.jbpm.api.HistoryService;
import org.jbpm.api.IdentityService;
import org.jbpm.api.JbpmException;
import org.jbpm.api.ManagementService;
import org.jbpm.api.ProcessEngine;
import org.jbpm.api.RepositoryService;
import org.jbpm.api.TaskService;
import org.jbpm.api.cmd.Command;
import org.jbpm.internal.log.Log;
import org.jbpm.pvm.internal.cfg.ConfigurationImpl;
import org.jbpm.pvm.internal.cmd.CheckDbCmd;
import org.jbpm.pvm.internal.cmd.CommandService;
import org.jbpm.pvm.internal.env.Context;
import org.jbpm.pvm.internal.env.EnvironmentFactory;
import org.jbpm.pvm.internal.env.EnvironmentImpl;
import org.jbpm.pvm.internal.env.PvmEnvironment;
import org.jbpm.pvm.internal.env.UserProvidedEnvironmentObject;
import org.jbpm.pvm.internal.jobexecutor.JobExecutor;
import org.jbpm.pvm.internal.wire.WireContext;
import org.jbpm.pvm.internal.wire.WireDefinition;
 
/**
 * an environment factory that also is the process-engine context.
 * 
 * <p>
 * This environment factory will produce environments with 2 contexts: the
 * process-engine context and the block context.
 * </p>
 * 
 * <p>
 * An process-engine context is build from two wire definitions: the
 * process-engine wire definition and the environment wire definition.
 * </p>
 * 
 * <p>
 * The process-engine context itself is build from the process-engine
 * wire definition. So all objects that are created in this context remain
 * cached for the lifetime of this process-engine context object.
 * </p>
 * 
 * <p>
 * This process-engine context is also a environment factory. The produced
 * environments contain 2 contexts: the process-engine context itself and a
 * new environment context, build from the environment wire definition. For each
 * created environment, a new environment context will be created from the same
 * environment wire definition. Objects in the environment context will live for
 * as long as the environment.
 * </p>
 * 
 * @author Tom Baeyens
 */
public class ProcessEngineImpl implements Context, ProcessEngine, EnvironmentFactory, Serializable {
 
  private static final long serialVersionUID = 1L;
  private static final Log log = Log.getLog(ProcessEngineImpl.class.getName());
  
  public static final String JBPM_LIBRARY_VERSION = "4.3";
 
  transient protected WireContext processEngineWireContext;
  transient protected WireDefinition transactionWireDefinition;
  
  transient protected ThreadLocal<List<UserProvidedEnvironmentObject>> userProvidedEnvironmentObjectsThreadLocal = new ThreadLocal<List<UserProvidedEnvironmentObject>>();
  transient protected ThreadLocal<String> authenticatedUserIdThreadLocal = new ThreadLocal<String>();
  
  transient protected CommandService userCommandService = null;
  
  public ProcessEngineImpl() {
  }
 
  public ProcessEngineImpl(ConfigurationImpl configuration) {
    initializeProcessEngine(configuration);
    checkDb(configuration);
  }
 
  protected void initializeProcessEngine(ConfigurationImpl configuration) {
    configuration.setProducedProcessEngine(this);
    this.processEngineWireContext = configuration.getProcessEngineWireContext();
    this.transactionWireDefinition = configuration.getTransactionWireDefinition();
    
    if (log.isTraceEnabled()) {
      log.trace("created ProcessEngine "+System.identityHashCode(this));
      if ( (processEngineWireContext!=null)
           && (processEngineWireContext.getWireDefinition()!=null)
           && (processEngineWireContext.getWireDefinition().getDescriptorTypes()!=null)
         ) {
        log.trace("  process-engine-context "+System.identityHashCode(processEngineWireContext));
        for (Class<?> descriptorType: processEngineWireContext.getWireDefinition().getDescriptorTypes()) {
          log.trace("    "+descriptorType.getName());
        }
      }
      if ( (transactionWireDefinition!=null)
           && (transactionWireDefinition.getDescriptorTypes()!=null)
         ) {
        log.trace("  transaction-context:");
        for (Class<?> descriptorType: transactionWireDefinition.getDescriptorTypes()) {
          log.trace("    "+descriptorType.getName());
        }
      }
    }
 
    processEngineWireContext.create();
    userCommandService = (CommandService) processEngineWireContext.get(CommandService.NAME_TX_REQUIRED_COMMAND_SERVICE);
 
    String jndiName = configuration.getJndiName();
    if (jndiName!=null) {
      try {
        log.debug("publishing jBPM ProcessEngine in jndi at "+jndiName);
        InitialContext initialContext = new InitialContext();
        initialContext.bind(jndiName, this);
      } catch (NamingException e) {
        throw new JbpmException("JNDI binding problem", e);
      }
    }
 
  }
 
  protected void checkDb(ConfigurationImpl configuration) {
    if (configuration.isCheckDb()) {
      userCommandService.execute(new CheckDbCmd());
    }
  }
 
  public ExecutionService getExecutionService() {
    return get(ExecutionService.class);
  }
  public HistoryService getHistoryService() {
    return get(HistoryService.class);
  }
  public ManagementService getManagementService() {
    return get(ManagementService.class);
  }
  public TaskService getTaskService() {
    return get(TaskService.class);
  }
  public IdentityService getIdentityService() {
    return get(IdentityService.class);
  }
  public RepositoryService getRepositoryService() {
    return get(RepositoryService.class);
  }
 
  public EnvironmentImpl openEnvironment() {
    PvmEnvironment environment = new PvmEnvironment(this);
 
    if (log.isTraceEnabled()) log.trace("opening " + environment);
 
    installAuthenticatedUserId(environment);
    installProcessEngineContext(environment);
    installTransactionContext(environment);
 
    return environment;
  }
 
  protected void installAuthenticatedUserId(EnvironmentImpl environment) {
    String authenticatedUserId = authenticatedUserIdThreadLocal.get();
    if (authenticatedUserId!=null) {
      environment.setAuthenticatedUserId(authenticatedUserId);
      authenticatedUserIdThreadLocal.set(null);
    }
  }
 
  protected void installTransactionContext(PvmEnvironment environment) {
    WireContext transactionContext = new WireContext(transactionWireDefinition, Context.CONTEXTNAME_TRANSACTION, true);
    // add the environment block context to the environment
    environment.setContext(transactionContext);
 
    EnvironmentImpl.pushEnvironment(environment);
    try {
      // finish the creation of the environment wire context
      transactionContext.create();
 
    } catch (RuntimeException e) {
      EnvironmentImpl.popEnvironment();
      throw e;
    }
  }
 
  protected void installProcessEngineContext(PvmEnvironment environment) {
    // add the process-engine context
    environment.setContext(processEngineWireContext);
  }
 
  public void close() {
    JobExecutor jobExecutor = get(JobExecutor.class);
    if (jobExecutor!=null) {
      // stop the job executor and wait till all job executor threads have stopped.
      jobExecutor.stop(true);
    }
    processEngineWireContext.fire(WireContext.EVENT_CLOSE, null);
  }
 
  // process-engine context delegation methods
  // ///////////////////////////////////
 
  public Object get(String key) {
    return processEngineWireContext.get(key);
  }
 
  public <T> T get(Class<T> type) {
    return processEngineWireContext.get(type);
  }
 
  public String getName() {
    return processEngineWireContext.getName();
  }
 
  public boolean has(String key) {
    return processEngineWireContext.has(key);
  }
 
  public Set<String> keys() {
    return processEngineWireContext.keys();
  }
 
  public Object set(String key, Object value) {
    return processEngineWireContext.set(key, value);
  }
  
  public void addProcessEngineWireDefinition(WireDefinition wireDefinition) {
    processEngineWireContext.getWireDefinition().addWireDefinition(wireDefinition);
  }
 
  public void addTransactionWireDefinition(WireDefinition wireDefinition) {
    transactionWireDefinition.addWireDefinition(wireDefinition);
  }
 
  // getters and setters //////////////////////////////////////////////////////
 
  public void setTransactionWireDefinition(WireDefinition transactionWireDefinition) {
    this.transactionWireDefinition = transactionWireDefinition;
  }
  public WireContext getProcessEngineWireContext() {
    return processEngineWireContext;
  }
  public void setProcessEngineWireContext(WireContext processEngineWireContext) {
    this.processEngineWireContext = processEngineWireContext;
  }
  public WireDefinition getTransactionWireDefinition() {
    return transactionWireDefinition;
  }
 
  public ProcessEngine setAuthenticatedUserId(String authenticatedUserId) {
    authenticatedUserIdThreadLocal.set(authenticatedUserId);
    return this;
  }
 
  public ProcessEngine setHibernateSession(Object hibernateSession) {
    addUserProvidedEnvironmentObject(new UserProvidedEnvironmentObject(hibernateSession, null, true));
    return this;
  }
 
  public ProcessEngine setJdbcConnection(Connection jdbcConnection) {
    addUserProvidedEnvironmentObject(new UserProvidedEnvironmentObject(jdbcConnection, null, true));
    return this;
  }
 
  protected synchronized void addUserProvidedEnvironmentObject(UserProvidedEnvironmentObject userProvidedEnvironmentObject) {
    List<UserProvidedEnvironmentObject> environmentObjects = userProvidedEnvironmentObjectsThreadLocal.get();
    if (environmentObjects==null) {
      environmentObjects = new ArrayList<UserProvidedEnvironmentObject>();
      userProvidedEnvironmentObjectsThreadLocal.set(environmentObjects);
    }
    environmentObjects.add(userProvidedEnvironmentObject);
  }
 
  public <T> T execute(Command<T> command) {
    return userCommandService.execute(command);
  }
 
  // left in for legacy test code
  public static EnvironmentFactory parseXmlString(String jbpmConfigurationXml) {
    return (EnvironmentFactory) new ConfigurationImpl()
        .setXmlString(jbpmConfigurationXml)
        .skipDbCheck()
        .buildProcessEngine();
  }
}