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
/*
 * 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.wire.binding;
 
import java.io.File;
import java.io.InputStream;
import java.net.URL;
import java.util.Enumeration;
import java.util.List;
 
import org.hibernate.cfg.Configuration;
import org.jbpm.internal.log.Log;
import org.jbpm.pvm.internal.stream.FileStreamInput;
import org.jbpm.pvm.internal.stream.ResourceStreamInput;
import org.jbpm.pvm.internal.stream.StreamInput;
import org.jbpm.pvm.internal.stream.UrlStreamInput;
import org.jbpm.pvm.internal.util.ReflectUtil;
import org.jbpm.pvm.internal.util.XmlUtil;
import org.jbpm.pvm.internal.wire.descriptor.HibernateConfigurationDescriptor;
import org.jbpm.pvm.internal.wire.descriptor.PropertiesDescriptor;
import org.jbpm.pvm.internal.xml.Parse;
import org.jbpm.pvm.internal.xml.Parser;
import org.w3c.dom.Element;
 
/** parses a descriptor for creating a hibernate Configuration.
 * 
 * See schema docs for more details.
 * 
 * @author Tom Baeyens
 */
public class HibernateConfigurationBinding extends WireDescriptorBinding {
 
  private static final Log log = Log.getLog(HibernateConfigurationBinding.class.getName());
  
  private static final PropertiesBinding propertiesBinding = new PropertiesBinding();
 
  public HibernateConfigurationBinding() {
    super("hibernate-configuration");
  }
 
  protected HibernateConfigurationBinding(String tagName) {
    super(tagName);
  }
 
  public Object parse(Element element, Parse parse, Parser parser) {
    HibernateConfigurationDescriptor descriptor = new HibernateConfigurationDescriptor();
    
    String configurationClassName = null;
    if ( element.hasAttribute("annotations")
         && element.getAttribute("annotations").equalsIgnoreCase("enabled")   
       ) {
      // don't replace the string with the reflective classname as that will 
      // introduce a hard dependency on the hibernate annotations library
      configurationClassName = "org.hibernate.cfg.AnnotationConfiguration";
 
    } else {
      configurationClassName = Configuration.class.getName();
    }
 
    descriptor.setClassName(configurationClassName);
 
    parseConfiguration(element, parse, descriptor, parser);
    
    return descriptor;
  }
 
  private void parseConfiguration(Element element, Parse parse, HibernateConfigurationDescriptor descriptor, Parser parser)
  {
    List<Element> configElements = XmlUtil.elements(element);
    for (Element configElement: configElements) {
 
      if ("cfg".equals(XmlUtil.getTagLocalName(configElement))) {
        if (configElement.hasAttribute("resource")) {
          String resource = configElement.getAttribute("resource");
          log.trace("adding hibernate configuration resource "+resource);
          descriptor.addCfgResource(resource);
 
        } else if (configElement.hasAttribute("file")) {
          String fileName = configElement.getAttribute("file");
          log.trace("adding hibernate configuration file "+fileName);
          descriptor.addCfgFile(fileName);
 
        } else if (configElement.hasAttribute("url")) {
          String urlText = configElement.getAttribute("url");
          log.trace("adding hibernate configuration url "+urlText);
          descriptor.addCfgUrl(urlText);
          
        } else {
          parse.addProblem("exactly 1 attribute in {resource, file, url} was expected in cfg: "+XmlUtil.toString(configElement), element);
        }
 
      } else if ("mapping".equals(XmlUtil.getTagLocalName(configElement))) {
        if (configElement.hasAttribute("resource")) {
          String resource = configElement.getAttribute("resource");
          log.trace("adding hibernate mapping resource "+resource);
          descriptor.addMappingResource(resource);
 
        } else if (configElement.hasAttribute("file")) {
          String fileName = configElement.getAttribute("file");
          log.trace("adding hibernate mapping file "+fileName);
          descriptor.addMappingFile(fileName);
 
        } else if (configElement.hasAttribute("class")) {
          String className = configElement.getAttribute("class");
          log.trace("adding hibernate mapping class "+className);
          descriptor.addMappingClass(className);
          
        } else if (configElement.hasAttribute("url")) {
          String urlText = configElement.getAttribute("url");
          log.trace("adding hibernate mapping url "+urlText);
          descriptor.addMappingUrl(urlText);
          
        } else {
          parse.addProblem("exactly 1 attribute in {resource, file, class, url} was expected in mapping: "+XmlUtil.toString(element));
        }
 
      } else if ("properties".equals(XmlUtil.getTagLocalName(configElement))) {
        PropertiesDescriptor propertiesDescriptor = (PropertiesDescriptor) propertiesBinding.parse(configElement, parse, parser);
        descriptor.setPropertiesDescriptor(propertiesDescriptor);
 
      } else if ("cache-configuration".equals(XmlUtil.getTagLocalName(configElement))) {
        StreamInput streamSource = null;
 
        String cacheUsage = configElement.getAttribute("usage");
        if (! ( ("read-only".equals(cacheUsage))
                || ("nonstrict-read-write".equals(cacheUsage))
                || ("read-write".equals(cacheUsage))
                || ("transactional".equals(cacheUsage))
              )
           ){
          parse.addProblem("problem in cache-configuration: no usage attribute or illegal value: "+cacheUsage+" Possible values are {read-only, nonstrict-read-write, read-write, transactional}");
        } else {
 
          if (configElement.hasAttribute("file")) {
            String fileName = configElement.getAttribute("file");
            File file = new File(fileName);
            if (file.exists() && file.isFile()) {
              streamSource = new FileStreamInput(file);
            } else {
              parse.addProblem("file "+fileName+" isn't a file");
            }
          }
 
          if (configElement.hasAttribute("resource")) {
            String resource = configElement.getAttribute("resource");
            
            ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
            streamSource = new ResourceStreamInput(resource, classLoader);
          }
 
          if (configElement.hasAttribute("url")) {
            String urlText = configElement.getAttribute("url");
            try {
              URL url = new URL(urlText);
              streamSource = new UrlStreamInput(url);
            } catch (Exception e) {
              parse.addProblem("couldn't open url "+urlText, e);
            }
          }
 
          if (streamSource != null) {
            parser.importStream(streamSource, configElement, parse);
          }
 
          // parse the cache configurations in the same way as the hibernate cfg schema
          // translate the contents of the file into invoke operations for methods
          // Configuration.setCacheConcurrencyStrategy(String clazz, String concurrencyStrategy, String region)
          // Configuration.setCollectionCacheConcurrencyStrategy(String collectionRole, String concurrencyStrategy)
          //         <class-cache class="org.hibernate.auction.Item" usage="read-write"/>
          //         <class-cache class="org.hibernate.auction.Bid" usage="read-only"/>
          //         <collection-cache collection="org.hibernate.auction.Item.bids" usage="read-write"/>
          List<Element> cacheElements = XmlUtil.elements(configElement);
          if (cacheElements!=null) {
            for (Element cacheElement : cacheElements) {
 
              if ("class-cache".equals(XmlUtil.getTagLocalName(cacheElement))) {
                String className = cacheElement.getAttribute("class");
                descriptor.addClassToCache(className, cacheUsage);
 
              } else if ("collection-cache".equals(XmlUtil.getTagLocalName(cacheElement))) {
                String collection = cacheElement.getAttribute("collection");
                descriptor.addCollectionToCache(collection, cacheUsage);
 
              } else {
                parse.addProblem("unknown hibernate cache configuration element "+XmlUtil.toString(configElement));
              }
            }
          }
        }
 
      } else {
        parse.addProblem("unknown hibernate configuration element "+XmlUtil.toString(configElement));
      }
    }
  }
 
}