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
package org.jbpm.pvm.internal.wire.descriptor;
 
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.SortedMap;
import java.util.SortedSet;
 
import org.jbpm.internal.log.Log;
import org.jbpm.pvm.internal.wire.Descriptor;
import org.jbpm.pvm.internal.wire.WireContext;
import org.jbpm.pvm.internal.wire.WireException;
 
 
/**
 * @author Tom Baeyens
 */
public class CollectionDescriptor extends AbstractDescriptor implements Descriptor {
 
  private static final long serialVersionUID = 1L;
 
  private static Log log = Log.getLog(CollectionDescriptor.class.getName());
  
  protected String className;
  protected List<Descriptor> valueDescriptors;
  protected boolean isSynchronized;
 
  protected CollectionDescriptor() { }
  
  public CollectionDescriptor(String defaultImplClassName) {
    this.className = defaultImplClassName;
  }
 
  public Object construct(WireContext wireContext) {
    Object object = null;
    try {
      // instantiate
      ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
      Class<?> clazz = Class.forName(className, true, classLoader);
      object = clazz.newInstance();
      
      if (isSynchronized) {
        if (object instanceof SortedSet) {
          object = Collections.synchronizedSortedSet((SortedSet) object);
        } else if (object instanceof SortedMap) {
          object = Collections.synchronizedSortedMap((SortedMap) object);
        } else if (object instanceof Set) {
          object = Collections.synchronizedSet((Set) object);
        } else if (object instanceof Map) {
          object = Collections.synchronizedMap((Map) object);
        } else if (object instanceof List) {
          object = Collections.synchronizedList((List) object);
        } else if (object instanceof Collection) {
          object = Collections.synchronizedCollection((Collection) object);
        }
      }
 
    } catch (Exception e) {
      throw new WireException("couldn't create '"+(name!=null ? name : className)+"': "+e.getMessage(), e);
    }
    return object;
  }
 
  public void initialize(Object object, WireContext wireContext) {
    Collection<Object> collection = (Collection<Object>) object;
    try {
      if (valueDescriptors!=null) {
        for(Descriptor descriptor: valueDescriptors) {
          Object element = wireContext.create(descriptor, true);
          log.trace("adding element "+element+" to collection");
          collection.add(element);
        }
      }
    } catch (WireException e) {
      throw e;
    } catch (Exception e) {
      throw new WireException("couldn't initialize object '"+(name!=null ? name : className)+"'", e);
    }
  }
 
  public void addValueDescriptors(List<Descriptor> otherValueDescriptors) {
    if (valueDescriptors==null) {
      valueDescriptors = new ArrayList<Descriptor>();
    }
    if (otherValueDescriptors!=null) {
      valueDescriptors.addAll(otherValueDescriptors);
    }
  }
 
  public String getClassName() {
    return className;
  }
  public void setClassName(String className) {
    this.className = className;
  }
  public List<Descriptor> getValueDescriptors() {
    return valueDescriptors;
  }
  public void setValueDescriptors(List<Descriptor> valueDescriptors) {
    this.valueDescriptors = valueDescriptors;
  }
  public boolean isSynchronized() {
    return isSynchronized;
  }
  public void setSynchronized(boolean isSynchronized) {
    this.isSynchronized = isSynchronized;
  }
}