3a731558a494f4e0158275bd50271427ad0649ea
[so.git] / bpmn / MSOCoreBPMN / src / main / java / org / openecomp / mso / bpmn / core / mybatis / CustomMyBatisSessionFactory.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * OPENECOMP - MSO
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.openecomp.mso.bpmn.core.mybatis;
22
23 import java.io.InputStream;
24 import java.util.ArrayList;
25 import java.util.Collection;
26 import java.util.List;
27
28 import org.camunda.bpm.engine.impl.cfg.ProcessEngineConfigurationImpl;
29 import org.camunda.bpm.engine.impl.cfg.StandaloneProcessEngineConfiguration;
30 import org.camunda.bpm.engine.impl.interceptor.CommandContextInterceptor;
31 import org.camunda.bpm.engine.impl.interceptor.CommandInterceptor;
32 import org.camunda.bpm.engine.impl.interceptor.LogInterceptor;
33 import org.camunda.bpm.engine.impl.util.ReflectUtil;
34
35
36 /**
37  * A special process engine that provides access to MyBatis mappings.
38  * @version 1.0
39  */
40 public class CustomMyBatisSessionFactory extends
41                 StandaloneProcessEngineConfiguration {
42
43         private String resourceName;
44
45         /**
46          * Overridden to ensure nobody ever tries to initialize this process engine
47          * in the normal way.  We are using this process engine only for MyBatis
48          * access.
49          */
50         @Override
51         protected void init() {
52                 throw new UnsupportedOperationException("init");
53         }
54
55         /**
56          * Initialize the ProcessEngineConfiguration from an existing one, just
57          * using the database settings to initialize the database / MyBatis stuff.
58          */
59         public void initFromProcessEngineConfiguration(
60                         ProcessEngineConfigurationImpl processEngineConfiguration,
61                         String resourceName) {
62                 this.resourceName = resourceName;
63
64                 setDatabaseType(processEngineConfiguration.getDatabaseType());
65                 setDataSource(processEngineConfiguration.getDataSource());
66                 setDatabaseTablePrefix(processEngineConfiguration
67                                 .getDatabaseTablePrefix());
68
69                 initDataSource();
70                 // initVariableTypes();
71                 initCommandContextFactory();
72                 initTransactionFactory();
73                 initTransactionContextFactory();
74                 initCommandExecutors();
75                 initSqlSessionFactory();
76                 initIncidentHandlers();
77                 initIdentityProviderSessionFactory();
78                 initSessionFactories();
79         }
80
81         /**
82          * In order to always open a new command context set the property
83          * "alwaysOpenNew" to true inside the CommandContextInterceptor.
84          * 
85          * If you execute the custom queries inside the process engine (for example
86          * in a service task), you have to do this.
87          */
88         @Override
89         protected Collection<? extends CommandInterceptor> getDefaultCommandInterceptorsTxRequired() {
90                 List<CommandInterceptor> defaultCommandInterceptorsTxRequired =
91                                 new ArrayList<CommandInterceptor>();
92                 defaultCommandInterceptorsTxRequired.add(new LogInterceptor());
93                 defaultCommandInterceptorsTxRequired.add(new CommandContextInterceptor(
94                                 commandContextFactory, this, true));
95                 return defaultCommandInterceptorsTxRequired;
96         }
97
98         @Override
99         protected InputStream getMyBatisXmlConfigurationSteam() {
100                 return ReflectUtil.getResourceAsStream(resourceName);
101         }
102 }