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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
/*
 * 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.repository;
 
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectStreamException;
import java.io.Serializable;
import java.net.URL;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
 
import org.jbpm.api.Deployment;
import org.jbpm.api.NewDeployment;
import org.jbpm.api.JbpmException;
import org.jbpm.pvm.internal.cmd.CommandService;
import org.jbpm.pvm.internal.cmd.DeployCmd;
import org.jbpm.pvm.internal.env.EnvironmentImpl;
import org.jbpm.pvm.internal.id.DbidGenerator;
import org.jbpm.pvm.internal.lob.Lob;
import org.jbpm.pvm.internal.stream.ByteArrayStreamInput;
import org.jbpm.pvm.internal.stream.FileStreamInput;
import org.jbpm.pvm.internal.stream.InputStreamInput;
import org.jbpm.pvm.internal.stream.ResourceStreamInput;
import org.jbpm.pvm.internal.stream.StreamInput;
import org.jbpm.pvm.internal.stream.StringStreamInput;
import org.jbpm.pvm.internal.stream.UrlStreamInput;
import org.jbpm.pvm.internal.util.IoUtil;
import org.jbpm.pvm.internal.xml.ProblemList;
 
 
/**
 * @author Tom Baeyens
 */
public class DeploymentImpl extends ProblemList implements NewDeployment, Serializable {
  
  private static final long serialVersionUID = 1L;
 
  public static final String KEY_PROCESS_LANGUAGE_ID = "langid";
  public static final String KEY_PROCESS_DEFINITION_ID = "pdid";
  public static final String KEY_PROCESS_DEFINITION_KEY = "pdkey";
  public static final String KEY_PROCESS_DEFINITION_VERSION = "pdversion";
  
  protected long dbid;
  protected String name;
  protected long timestamp;
  protected String state = Deployment.STATE_ACTIVE;
  protected Map<String, Lob> resources;
 
  protected CommandService commandService;
  protected Map<String, Object> objects;
  protected Set<DeploymentProperty> objectProperties;
 
  public DeploymentImpl() {
  }
 
  public DeploymentImpl(CommandService commandService) {
    this.commandService = commandService;
  }
  
  public String toString() {
    return "deployment("+dbid+")";
  }
  
  public String deploy() {
    return commandService.execute(new DeployCmd(this));
  }
  
  public NewDeployment addResourceFromClasspath(String resourceName) {
    addResourceFromStreamInput(resourceName, new ResourceStreamInput(resourceName));
    return this;
  }
 
  public NewDeployment addResourceFromString(String resourceName, String text) {
    addResourceFromStreamInput(resourceName, new StringStreamInput(text));
    return this;
  }
  
  public NewDeployment addResourcesFromZipInputStream(ZipInputStream zipInputStream) {
    try {
      ZipEntry zipEntry = zipInputStream.getNextEntry();
      while(zipEntry!=null) {
        String entryName = zipEntry.getName();
        byte[] bytes = IoUtil.readBytes(zipInputStream);
        if (bytes!=null) {
          addResourceFromStreamInput(entryName, new ByteArrayStreamInput(bytes));
        }
        zipEntry = zipInputStream.getNextEntry();
      }
    } catch (Exception e) {
      throw new JbpmException("couldn't read zip archive", e);
    }
    return this;
  }
 
  public NewDeployment addResourceFromInputStream(String resourceName, InputStream inputStream) {
    addResourceFromStreamInput(resourceName, new InputStreamInput(inputStream));
    return this;
  }
 
  public NewDeployment addResourceFromUrl(URL url) {
    addResourceFromStreamInput(url.toString(), new UrlStreamInput(url));
    return this;
  }
 
  public NewDeployment addResourceFromFile(File file) {
    addResourceFromStreamInput(file.getPath(), new FileStreamInput(file));
    return this;
  }
 
  public NewDeployment addResourceFromStreamInput(String name, StreamInput streamInput) {
    if (resources==null) {
      resources = new HashMap<String, Lob>();
    }
    byte[] bytes = null;
    try {
      InputStream inputStream = streamInput.openStream();
      bytes = IoUtil.readBytes(inputStream);
      inputStream.close();
    } catch (IOException e) {
      throw new JbpmException("couldn't read from "+streamInput, e);
    }
    
    // Since this method is probably called outside an environment block, we 
    // need to generate the dbid of the Lob later (during the actual deployment).
    Lob lob = new Lob(bytes, false); 
    resources.put(name, lob);
    
    return this;
  }
 
  public Set<String> getResourceNames() {
    if (resources==null) {
      return Collections.emptySet();
    }
    return resources.keySet();
  }
  
  /**
   * This method should be called before saving the deployment. It will assign a
   * generated dbid to the resource Lobs.
   * 
   * Note: when using a database, this method must be called within an
   * environment block!
   */
  public void initResourceLobDbids() {
    for (Lob resource : resources.values()) {
      long resourceDbid = DbidGenerator.getDbidGenerator().getNextId();
      resource.setDbid(resourceDbid);      
    }
  }
  
  public byte[] getBytes(String resourceName) {
    if (resources!=null) {
      Lob lob = resources.get(resourceName);
      if (lob!=null) {
        return lob.extractBytes();
      }
    }
    return null;
  }
 
  public void addObject(String objectName, Object object) {
    if (objects==null) {
      objects = new HashMap<String, Object>();
    }
    objects.put(objectName, object);
  }
  
  // object properties ////////////////////////////////////////////////////////
  
  public void setProcessDefinitionId(String processDefinitionName, String processDefinitionId) {
    setObjectProperty(processDefinitionName, KEY_PROCESS_DEFINITION_ID, processDefinitionId);
  }
  
  public String getProcessDefinitionId(String processDefinitionName) {
    return (String) getObjectProperty(processDefinitionName, KEY_PROCESS_DEFINITION_ID);
  }
 
  public void setProcessDefinitionKey(String processDefinitionName, String processDefinitionKey) {
    setObjectProperty(processDefinitionName, KEY_PROCESS_DEFINITION_KEY, processDefinitionKey);
  }
 
  public String getProcessDefinitionKey(String processDefinitionName) {
    return (String) getObjectProperty(processDefinitionName, KEY_PROCESS_DEFINITION_KEY);
  }
 
  public void setProcessDefinitionVersion(String processDefinitionName, Long processDefinitionVersion) {
    setObjectProperty(processDefinitionName, KEY_PROCESS_DEFINITION_VERSION, processDefinitionVersion);
  }
  
  public Long getProcessDefinitionVersion(String processDefinitionName) {
    return (Long) getObjectProperty(processDefinitionName, KEY_PROCESS_DEFINITION_VERSION);
  }
  
  public String getProcessLanguageId(String processDefinitionName) {
    return (String) getObjectProperty(processDefinitionName, KEY_PROCESS_LANGUAGE_ID);
  }
 
  public void setProcessLanguageId(String processDefinitionName, String processLanguageId) {
    setObjectProperty(processDefinitionName, KEY_PROCESS_LANGUAGE_ID, processLanguageId);
  }
 
  public void setObjectProperty(String objectName, String key, Object value) {
    if (objectProperties==null) {
      objectProperties = new HashSet<DeploymentProperty>();
    }
    DeploymentProperty deploymentProperty = new DeploymentProperty(this, objectName, key);
    deploymentProperty.setValue(value);
    objectProperties.add(deploymentProperty);
  }
 
  public Object removeObjectProperty(String objectName, String key) {
    if (objectProperties != null) {
      for (DeploymentProperty deploymentProperty : objectProperties) {
        if (deploymentProperty.getObjectName().equals(objectName)
            && deploymentProperty.getKey().equals(key)) {
          Object value = deploymentProperty.getValue();
          objectProperties.remove(deploymentProperty);
          return value;
        }
      }
    }
    return null;
  }
 
  public Object getObjectProperty(String objectName, String key) {
    if (objectProperties!=null) {
      for (DeploymentProperty deploymentProperty: objectProperties) {
        if ( (deploymentProperty.getObjectName().equals(objectName))
             && (deploymentProperty.getKey().equals(key))
           ) {
          return deploymentProperty.getValue();
        }
      }
    }
    return null;
  }
 
  public Set<String> getProcessDefinitionIds() {
    Set<String> processDefinitionIds = new HashSet<String>();
    if (objectProperties!=null) {
      for (DeploymentProperty deploymentProperty: objectProperties) {
        if (KEY_PROCESS_DEFINITION_ID.equals(deploymentProperty.getKey())) {
          String processDefinitionId = deploymentProperty.getStringValue();
          processDefinitionIds.add(processDefinitionId);
        }
      }
    }
    return processDefinitionIds;
  }
 
  public boolean hasObjectProperties(String objectName) {
    if (objectProperties!=null) {
      for (DeploymentProperty deploymentProperty: objectProperties) {
        if ( deploymentProperty.getObjectName().equals(objectName)
             && KEY_PROCESS_DEFINITION_ID.equals(deploymentProperty.getKey())
           ) {
          return true;
        }
      }
    }
    return false;
  }
  
  public void suspend() {
    if (isSuspended()) {
      throw new JbpmException("deployment is already suspended");
    }
    
    state = Deployment.STATE_SUSPENDED;
 
    RepositorySessionImpl repositorySession = EnvironmentImpl.getFromCurrent(RepositorySessionImpl.class);
    repositorySession.cascadeDeploymentSuspend(this);
 
    RepositoryCache repositoryCache = EnvironmentImpl.getFromCurrent(RepositoryCache.class);
    repositoryCache.remove(Long.toString(dbid));
  }
 
  public void resume() {
    if (!isSuspended()) {
      throw new JbpmException("deployment is not suspended");
    }
    
    state = Deployment.STATE_ACTIVE;
    
    RepositorySessionImpl repositorySession = EnvironmentImpl.getFromCurrent(RepositorySessionImpl.class);
    repositorySession.cascadeDeploymentResume(this);
    
    RepositoryCache repositoryCache = EnvironmentImpl.getFromCurrent(RepositoryCache.class);
    repositoryCache.remove(Long.toString(dbid));
  }
  
  public boolean isSuspended() {
    return Deployment.STATE_SUSPENDED.equals(state); 
  }
 
  protected Object writeReplace() throws ObjectStreamException {
    commandService = null;
    return this;
  }
 
  // customized getters and setters ///////////////////////////////////////////
  
  public String getId() {
    return Long.toString(dbid);
  }
  
  // getters and setters //////////////////////////////////////////////////////
 
  public long getDbid() {
    return dbid;
  }
  public Map<String, Object> getObjects() {
    return objects;
  }
  public Set<DeploymentProperty> getObjectProperties() {
    return objectProperties;
  }
  public String getName() {
    return name;
  }
  public DeploymentImpl setName(String name) {
    this.name = name;
    return this;
  }
  public long getTimestamp() {
    return timestamp;
  }
  public DeploymentImpl setTimestamp(long timestamp) {
    this.timestamp = timestamp;
    return this;
  }
  public String getState() {
    return state;
  }
  public void setDbid(long dbid) {
    this.dbid = dbid;
  }
}