catch up with dev
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..b9f0a46
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,37 @@
+**/bin/
+**/target/
+**._trace
+**git.properties
+**/target/
+**/.metadata/
+
+**/emf-gen/**/.gitignore
+**/src-gen/**/.gitignore
+**/git.properties
+**/*.java._trace
+**/*.log
+**/*.log.properties
+
+**/bin/
+**/build/
+*.class
+*.ctrl
+**/Debug/
+.DS_Store
+*.ear
+*.war
+
+**/*.actionbin
+**/*.blipbin
+**/*.chartbin
+**/*.cubebin
+**/*.datatypebin
+**/*.dtobin
+**/*.entitybin
+**/*.servicebin
+**/*.tablebin
+**/*.uibin
+**/*.uisemanticbin
+**/*.xtendbin
+**/*.xtextbin
+
diff --git a/.settings/org.eclipse.core.resources.prefs b/.settings/org.eclipse.core.resources.prefs
new file mode 100644
index 0000000..99f26c0
--- /dev/null
+++ b/.settings/org.eclipse.core.resources.prefs
@@ -0,0 +1,2 @@
+eclipse.preferences.version=1
+encoding/<project>=UTF-8
diff --git a/.settings/org.eclipse.m2e.core.prefs b/.settings/org.eclipse.m2e.core.prefs
new file mode 100644
index 0000000..f897a7f
--- /dev/null
+++ b/.settings/org.eclipse.m2e.core.prefs
@@ -0,0 +1,4 @@
+activeProfiles=
+eclipse.preferences.version=1
+resolveWorkspaceProjects=true
+version=1
diff --git a/jenkins.build.config.xml b/jenkins.build.config.xml
index afe9273..cfca11b 100644
--- a/jenkins.build.config.xml
+++ b/jenkins.build.config.xml
@@ -15,6 +15,7 @@
<jenkins>
<!-- DO NOT EDIT BELOW THIS LINE -->
<jenkins.build.dependencies>
+ <jenkins.build.dependency>org.eclipse.osbp.core.api</jenkins.build.dependency>
<jenkins.build.dependency>org.eclipse.osbp.mondrian.api</jenkins.build.dependency>
<jenkins.build.dependency>org.eclipse.osbp.preferences</jenkins.build.dependency>
<jenkins.build.dependency>org.eclipse.osbp.runtime</jenkins.build.dependency>
diff --git a/org.eclipse.osbp.persistence.feature/.gitignore b/org.eclipse.osbp.persistence.feature/.gitignore
new file mode 100644
index 0000000..b83d222
--- /dev/null
+++ b/org.eclipse.osbp.persistence.feature/.gitignore
@@ -0,0 +1 @@
+/target/
diff --git a/org.eclipse.osbp.persistence.feature/.settings/org.eclipse.core.resources.prefs b/org.eclipse.osbp.persistence.feature/.settings/org.eclipse.core.resources.prefs
new file mode 100644
index 0000000..99f26c0
--- /dev/null
+++ b/org.eclipse.osbp.persistence.feature/.settings/org.eclipse.core.resources.prefs
@@ -0,0 +1,2 @@
+eclipse.preferences.version=1
+encoding/<project>=UTF-8
diff --git a/org.eclipse.osbp.persistence.feature/.settings/org.eclipse.m2e.core.prefs b/org.eclipse.osbp.persistence.feature/.settings/org.eclipse.m2e.core.prefs
new file mode 100644
index 0000000..f897a7f
--- /dev/null
+++ b/org.eclipse.osbp.persistence.feature/.settings/org.eclipse.m2e.core.prefs
@@ -0,0 +1,4 @@
+activeProfiles=
+eclipse.preferences.version=1
+resolveWorkspaceProjects=true
+version=1
diff --git a/org.eclipse.osbp.persistence.feature/.settings/org.eclipse.xtend.core.Xtend.prefs b/org.eclipse.osbp.persistence.feature/.settings/org.eclipse.xtend.core.Xtend.prefs
new file mode 100644
index 0000000..0933f8c
--- /dev/null
+++ b/org.eclipse.osbp.persistence.feature/.settings/org.eclipse.xtend.core.Xtend.prefs
@@ -0,0 +1,10 @@
+//outlet.DEFAULT_OUTPUT.sourceFolder.src/test/java.directory=src/test/generated-sources/xtend
+eclipse.preferences.version=1
+is_project_specific=true
+outlet.DEFAULT_OUTPUT.hideLocalSyntheticVariables=true
+outlet.DEFAULT_OUTPUT.installDslAsPrimarySource=false
+outlet.DEFAULT_OUTPUT.sourceFolder.emf-gen.directory=xtend-gen
+outlet.DEFAULT_OUTPUT.sourceFolder.src-gen.directory=xtend-gen
+outlet.DEFAULT_OUTPUT.sourceFolder.src.directory=xtend-gen
+outlet.DEFAULT_OUTPUT.sourceFolder.xtend-gen.directory=xtend-gen
+outlet.DEFAULT_OUTPUT.userOutputPerSourceFolder=true
diff --git a/org.eclipse.osbp.persistence.feature/feature.xml b/org.eclipse.osbp.persistence.feature/feature.xml
index 6997030..0346a0e 100644
--- a/org.eclipse.osbp.persistence.feature/feature.xml
+++ b/org.eclipse.osbp.persistence.feature/feature.xml
@@ -14,7 +14,8 @@
id="org.eclipse.osbp.persistence.feature"
label="%featureName"
version="0.9.0.qualifier"
- provider-name="%providerName">
+ provider-name="%providerName"
+ plugin="org.eclipse.osbp.persistence">
<description>
%description
diff --git a/org.eclipse.osbp.persistence/.classpath b/org.eclipse.osbp.persistence/.classpath
new file mode 100644
index 0000000..43b9862
--- /dev/null
+++ b/org.eclipse.osbp.persistence/.classpath
@@ -0,0 +1,7 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<classpath>
+ <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
+ <classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/>
+ <classpathentry kind="src" path="src"/>
+ <classpathentry kind="output" path="target/classes"/>
+</classpath>
diff --git a/org.eclipse.osbp.persistence/.settings/org.eclipse.core.resources.prefs b/org.eclipse.osbp.persistence/.settings/org.eclipse.core.resources.prefs
new file mode 100644
index 0000000..99f26c0
--- /dev/null
+++ b/org.eclipse.osbp.persistence/.settings/org.eclipse.core.resources.prefs
@@ -0,0 +1,2 @@
+eclipse.preferences.version=1
+encoding/<project>=UTF-8
diff --git a/org.eclipse.osbp.persistence/.settings/org.eclipse.jdt.core.prefs b/org.eclipse.osbp.persistence/.settings/org.eclipse.jdt.core.prefs
new file mode 100644
index 0000000..0c68a61
--- /dev/null
+++ b/org.eclipse.osbp.persistence/.settings/org.eclipse.jdt.core.prefs
@@ -0,0 +1,7 @@
+eclipse.preferences.version=1
+org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
+org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
+org.eclipse.jdt.core.compiler.compliance=1.8
+org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
+org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
+org.eclipse.jdt.core.compiler.source=1.8
diff --git a/org.eclipse.osbp.persistence/.settings/org.eclipse.m2e.core.prefs b/org.eclipse.osbp.persistence/.settings/org.eclipse.m2e.core.prefs
new file mode 100644
index 0000000..f897a7f
--- /dev/null
+++ b/org.eclipse.osbp.persistence/.settings/org.eclipse.m2e.core.prefs
@@ -0,0 +1,4 @@
+activeProfiles=
+eclipse.preferences.version=1
+resolveWorkspaceProjects=true
+version=1
diff --git a/org.eclipse.osbp.persistence/.settings/org.eclipse.xtend.core.Xtend.prefs b/org.eclipse.osbp.persistence/.settings/org.eclipse.xtend.core.Xtend.prefs
new file mode 100644
index 0000000..0933f8c
--- /dev/null
+++ b/org.eclipse.osbp.persistence/.settings/org.eclipse.xtend.core.Xtend.prefs
@@ -0,0 +1,10 @@
+//outlet.DEFAULT_OUTPUT.sourceFolder.src/test/java.directory=src/test/generated-sources/xtend
+eclipse.preferences.version=1
+is_project_specific=true
+outlet.DEFAULT_OUTPUT.hideLocalSyntheticVariables=true
+outlet.DEFAULT_OUTPUT.installDslAsPrimarySource=false
+outlet.DEFAULT_OUTPUT.sourceFolder.emf-gen.directory=xtend-gen
+outlet.DEFAULT_OUTPUT.sourceFolder.src-gen.directory=xtend-gen
+outlet.DEFAULT_OUTPUT.sourceFolder.src.directory=xtend-gen
+outlet.DEFAULT_OUTPUT.sourceFolder.xtend-gen.directory=xtend-gen
+outlet.DEFAULT_OUTPUT.userOutputPerSourceFolder=true
diff --git a/org.eclipse.osbp.persistence/META-INF/MANIFEST.MF b/org.eclipse.osbp.persistence/META-INF/MANIFEST.MF
index e5ad56e..e0149b7 100644
--- a/org.eclipse.osbp.persistence/META-INF/MANIFEST.MF
+++ b/org.eclipse.osbp.persistence/META-INF/MANIFEST.MF
@@ -7,7 +7,7 @@
org.eclipse.core.runtime,
javax.persistence;bundle-version="2.1.0",
org.eclipse.osgi.services;bundle-version="3.4.0",
- org.eclipse.osbp.fork.gemini.naming;bundle-version="[1.1.0,1.1.1)",
+ org.eclipse.gemini.naming;bundle-version="1.0.5",
org.eclipse.persistence.core,
org.apache.commons.math,
org.apache.commons.collections;bundle-version="3.2.1",
@@ -26,31 +26,16 @@
org.eclipse.osbp.preferences;bundle-version="[0.9.0,0.10.0)",
org.eclipse.osgi.util;bundle-version="3.3.0",
org.eclipse.osbp.gitinfo;bundle-version="[0.9.0,0.10.0)",
- org.eclipse.osbp.runtime.common,
+ org.eclipse.osbp.runtime.common;bundle-version="[0.9.0,0.10.0)",
org.apache.felix.gogo.runtime,
javax.validation.api
Bundle-RequiredExecutionEnvironment: JavaSE-1.8
Bundle-Vendor: Eclipse OSBP
-Import-Package: org.eclipse.osbp.mondrian.api;version="0.9.0",
+Import-Package: org.eclipse.osbp.core.api.persistence;version="0.9.0",
+ org.eclipse.osbp.mondrian.api;version="0.9.0",
org.osgi.service.component;version="1.2.0",
org.osgi.service.jdbc;version="1.0.0"
-Export-Package: org.eclipse.osbp.persistence;version="0.9.0";
- uses:="org.osgi.framework,
- org.osgi.service.component,
- javax.persistence,
- mondrian.rolap,
- mondrian.olap,
- javax.sql",
- org.eclipse.osbp.persistence.impl;version="0.9.0";x-internal:=true;
- uses:="org.eclipse.osbp.persistence,
- org.osgi.service.component,
- javax.persistence,
- mondrian.rolap,
- org.eclipse.osbp.persistence.mondrian,
- javax.sql,
- org.osgi.framework,
- org.eclipse.persistence.jpa,
- mondrian.olap"
+Export-Package: org.eclipse.osbp.persistence
Service-Component: OSGI-INF/*.xml
Bundle-Activator: org.eclipse.osbp.persistence.Activator
Bundle-ActivationPolicy: lazy
diff --git a/org.eclipse.osbp.persistence/OSGI-INF/org.eclipse.osbp.persistence.impl.PersistenceService.xml b/org.eclipse.osbp.persistence/OSGI-INF/org.eclipse.osbp.persistence.PersistenceService.xml
similarity index 68%
rename from org.eclipse.osbp.persistence/OSGI-INF/org.eclipse.osbp.persistence.impl.PersistenceService.xml
rename to org.eclipse.osbp.persistence/OSGI-INF/org.eclipse.osbp.persistence.PersistenceService.xml
index 107afd6..a00f709 100644
--- a/org.eclipse.osbp.persistence/OSGI-INF/org.eclipse.osbp.persistence.impl.PersistenceService.xml
+++ b/org.eclipse.osbp.persistence/OSGI-INF/org.eclipse.osbp.persistence.PersistenceService.xml
@@ -1,9 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
-<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0" immediate="true" name="org.eclipse.osbp.persistence.impl.PersistenceService">
+<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0" name="org.eclipse.osbp.persistence.PersistenceService">
<service>
- <provide interface="org.eclipse.osbp.persistence.IPersistenceService"/>
+ <provide interface="org.eclipse.osbp.core.api.persistence.IPersistenceService"/>
</service>
<reference bind="bindMondrianProvider" cardinality="1..1" interface="org.eclipse.osbp.mondrian.api.IMondrianManager" name="MondrianProvider" policy="dynamic" unbind="unbindMondrianProvider"/>
<reference bind="bindValidationFactory" cardinality="1..1" interface="javax.validation.ValidatorFactory" name="ValidationFactory" policy="dynamic" unbind="unbindValidationFactory"/>
- <implementation class="org.eclipse.osbp.persistence.impl.PersistenceService"/>
+ <implementation class="org.eclipse.osbp.persistence.PersistenceService"/>
</scr:component>
\ No newline at end of file
diff --git a/org.eclipse.osbp.persistence/src/org/eclipse/osbp/persistence/impl/ArchiveFactoryImpl.java b/org.eclipse.osbp.persistence/src/org/eclipse/osbp/persistence/CustomArchiveFactory.java
similarity index 91%
rename from org.eclipse.osbp.persistence/src/org/eclipse/osbp/persistence/impl/ArchiveFactoryImpl.java
rename to org.eclipse.osbp.persistence/src/org/eclipse/osbp/persistence/CustomArchiveFactory.java
index 46c10dd..ae16307 100644
--- a/org.eclipse.osbp.persistence/src/org/eclipse/osbp/persistence/impl/ArchiveFactoryImpl.java
+++ b/org.eclipse.osbp.persistence/src/org/eclipse/osbp/persistence/CustomArchiveFactory.java
@@ -10,7 +10,7 @@
* Contributors:
* Oracle - initial API and implementation from Oracle TopLink
******************************************************************************/
-package org.eclipse.osbp.persistence.impl;
+package org.eclipse.osbp.persistence;
import java.io.File;
import java.io.IOException;
@@ -37,7 +37,7 @@
*
* @author Sanjeeb.Sahoo@Sun.COM
*/
-public class ArchiveFactoryImpl implements ArchiveFactory {
+public class CustomArchiveFactory implements ArchiveFactory {
/*
* Implementation Note: This class does not have any dependency on either
@@ -47,11 +47,11 @@
protected Logger logger;
@SuppressWarnings("deprecation")
- public ArchiveFactoryImpl() {
+ public CustomArchiveFactory() {
this(Logger.global);
}
- public ArchiveFactoryImpl(Logger logger) {
+ public CustomArchiveFactory(Logger logger) {
this.logger = logger;
}
@@ -79,7 +79,7 @@
e.printStackTrace();
}
}
- ArchiveFactoryImpl archiveFactoryImpl = new ArchiveFactoryImpl(logger);
+ CustomArchiveFactory archiveFactoryImpl = new CustomArchiveFactory(logger);
result = archiveFactoryImpl.createArchive(rootUrl, descriptorLocation, properties);
logger.exiting(className, "createArchive", result);
return result;
diff --git a/org.eclipse.osbp.persistence/src/org/eclipse/osbp/persistence/DataSourceFactoryException.java b/org.eclipse.osbp.persistence/src/org/eclipse/osbp/persistence/DataSourceFactoryException.java
new file mode 100644
index 0000000..b6d1677
--- /dev/null
+++ b/org.eclipse.osbp.persistence/src/org/eclipse/osbp/persistence/DataSourceFactoryException.java
@@ -0,0 +1,10 @@
+package org.eclipse.osbp.persistence;
+
+public class DataSourceFactoryException extends RuntimeException {
+
+ private static final long serialVersionUID = 1L;
+
+ public DataSourceFactoryException(String string, Throwable ex) {
+ super(string, ex);
+ }
+}
diff --git a/org.eclipse.osbp.persistence/src/org/eclipse/osbp/persistence/impl/DataSourceProperties.java b/org.eclipse.osbp.persistence/src/org/eclipse/osbp/persistence/DataSourceProperties.java
similarity index 95%
rename from org.eclipse.osbp.persistence/src/org/eclipse/osbp/persistence/impl/DataSourceProperties.java
rename to org.eclipse.osbp.persistence/src/org/eclipse/osbp/persistence/DataSourceProperties.java
index 62f186f..5ab908d 100644
--- a/org.eclipse.osbp.persistence/src/org/eclipse/osbp/persistence/impl/DataSourceProperties.java
+++ b/org.eclipse.osbp.persistence/src/org/eclipse/osbp/persistence/DataSourceProperties.java
@@ -11,11 +11,11 @@
* Christophe Loetz (Loetz GmbH&Co.KG) - initial implementation
*
*/
- package org.eclipse.osbp.persistence.impl;
+ package org.eclipse.osbp.persistence;
import java.util.Properties;
-import org.eclipse.osbp.persistence.IDataSourceProperties;
+import org.eclipse.osbp.core.api.persistence.IDataSourceProperties;
import org.eclipse.osbp.preferences.DataSourceConfiguration;
import org.eclipse.osbp.preferences.ProductConfiguration;
diff --git a/org.eclipse.osbp.persistence/src/org/eclipse/osbp/persistence/IDataSourceProperties.java b/org.eclipse.osbp.persistence/src/org/eclipse/osbp/persistence/IDataSourceProperties.java
deleted file mode 100644
index ef98d56..0000000
--- a/org.eclipse.osbp.persistence/src/org/eclipse/osbp/persistence/IDataSourceProperties.java
+++ /dev/null
@@ -1,31 +0,0 @@
-/**
- *
- * Copyright (c) 2011, 2016 - Loetz GmbH&Co.KG (69115 Heidelberg, Germany)
- *
- * All rights reserved. This program and the accompanying materials
- * are made available under the terms of the Eclipse Public License v1.0
- * which accompanies this distribution, and is available at
- * http://www.eclipse.org/legal/epl-v10.html
- *
- * Contributors:
- * Christophe Loetz (Loetz GmbH&Co.KG) - initial implementation
- *
- */
- package org.eclipse.osbp.persistence;
-
-import java.util.Properties;
-
-public interface IDataSourceProperties {
-
- public abstract String getDataSourceName();
-
- public abstract Properties getDataSourceProperties();
-
- public abstract void setDataSourceProperties(Properties props);
-
- public abstract void setDataSourceProperty(String key, String value);
-
- public abstract String getClientDriverName();
-
- public abstract boolean isReasonable();
-}
diff --git a/org.eclipse.osbp.persistence/src/org/eclipse/osbp/persistence/IPersistenceService.java b/org.eclipse.osbp.persistence/src/org/eclipse/osbp/persistence/IPersistenceService.java
deleted file mode 100644
index 044d596..0000000
--- a/org.eclipse.osbp.persistence/src/org/eclipse/osbp/persistence/IPersistenceService.java
+++ /dev/null
@@ -1,51 +0,0 @@
-/**
- *
- * Copyright (c) 2011, 2016 - Loetz GmbH&Co.KG (69115 Heidelberg, Germany)
- *
- * All rights reserved. This program and the accompanying materials
- * are made available under the terms of the Eclipse Public License v1.0
- * which accompanies this distribution, and is available at
- * http://www.eclipse.org/legal/epl-v10.html
- *
- * Contributors:
- * Christophe Loetz (Loetz GmbH&Co.KG) - initial implementation
- *
- */
- package org.eclipse.osbp.persistence;
-
-import java.sql.SQLException;
-import java.util.Map;
-
-import javax.persistence.EntityManagerFactory;
-import javax.sql.CommonDataSource;
-import javax.sql.XAConnection;
-
-import mondrian.olap.Result;
-import mondrian.rolap.RolapConnection;
-
-import org.osgi.framework.Filter;
-import org.osgi.framework.InvalidSyntaxException;
-import org.osgi.service.component.ComponentContext;
-
-// TODO goingEclipse - DOKU!
-public interface IPersistenceService {
- CommonDataSource getDataSource(String jndiName);
- java.sql.Connection getJndiConnection(String jndiName) throws SQLException;
- java.sql.Connection getPersistenceUnitConnection(String persistenceUnit) throws SQLException;
- XAConnection getXAJndiConnection(String jndiName) throws SQLException;
- XAConnection getXAPersistenceUnitConnection(String persistenceUnit) throws SQLException;
- RolapConnection getMondrianConnection(String persistenceUnit, String fullQualifiedCubePackageName) throws SQLException;
- Result sendQuery(RolapConnection con, String query);
- Result sendQuery(RolapConnection con, String query, long ms);
- Filter createEMFFilter(ComponentContext context, String persistenceUnitName) throws InvalidSyntaxException;
- void registerPersistenceUnit(String persistenceUnitName, Class<?> cls);
- EntityManagerFactory getEntityManagerFactory(String persistenceUnitName);
- /**
- * @return the local thread specific map of entity manager factories
- */
- Map<String, EntityManagerFactory> getEntityManagerFactoryMap();
- /**
- * @param entityManagerFactoryMap the specific map of entity manager factories to be inherited into the local thread
- */
- void inheritEntityManagerFactoryMap(Map<String, EntityManagerFactory> entityManagerFactoryMap);
-}
diff --git a/org.eclipse.osbp.persistence/src/org/eclipse/osbp/persistence/PersistenceService.java b/org.eclipse.osbp.persistence/src/org/eclipse/osbp/persistence/PersistenceService.java
new file mode 100644
index 0000000..bf27d5f
--- /dev/null
+++ b/org.eclipse.osbp.persistence/src/org/eclipse/osbp/persistence/PersistenceService.java
@@ -0,0 +1,473 @@
+/**
+ *
+ * Copyright (c) 2011, 2016 - Loetz GmbH&Co.KG (69115 Heidelberg, Germany)
+ *
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * Christophe Loetz (Loetz GmbH&Co.KG) - initial implementation
+ *
+ */
+package org.eclipse.osbp.persistence;
+
+import java.sql.SQLException;
+import java.util.ArrayList;
+import java.util.Dictionary;
+import java.util.HashMap;
+import java.util.Hashtable;
+import java.util.List;
+import java.util.Map;
+import java.util.Map.Entry;
+import java.util.Properties;
+
+import javax.naming.InitialContext;
+import javax.naming.NamingException;
+import javax.persistence.EntityManagerFactory;
+import javax.sql.CommonDataSource;
+import javax.sql.DataSource;
+import javax.sql.XAConnection;
+import javax.sql.XADataSource;
+import javax.validation.ValidatorFactory;
+
+import org.eclipse.osbp.core.api.persistence.IDataSourceProperties;
+import org.eclipse.osbp.core.api.persistence.IPersistenceException;
+import org.eclipse.osbp.core.api.persistence.IPersistenceService;
+import org.eclipse.osbp.mondrian.api.IMondrianManager;
+import org.eclipse.osbp.mondrian.api.IMondrianService;
+import org.eclipse.osbp.preferences.ProductConfiguration;
+import org.eclipse.persistence.config.PersistenceUnitProperties;
+import org.eclipse.persistence.config.SystemProperties;
+import org.eclipse.persistence.internal.jpa.deployment.ArchiveFactoryImpl;
+import org.eclipse.persistence.jpa.PersistenceProvider;
+import org.eclipse.persistence.sessions.Session;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.Filter;
+import org.osgi.framework.InvalidSyntaxException;
+import org.osgi.framework.ServiceReference;
+import org.osgi.framework.ServiceRegistration;
+import org.osgi.service.component.ComponentContext;
+import org.osgi.service.component.annotations.Component;
+import org.osgi.service.component.annotations.Reference;
+import org.osgi.service.component.annotations.ReferenceCardinality;
+import org.osgi.service.component.annotations.ReferencePolicy;
+import org.osgi.service.jdbc.DataSourceFactory;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import mondrian.olap.CacheControl;
+import mondrian.olap.Cube;
+import mondrian.olap.DriverManager;
+import mondrian.olap.Result;
+import mondrian.olap.Util.PropertyList;
+import mondrian.rolap.RolapConnection;
+import mondrian.server.Execution;
+
+@Component(service = IPersistenceService.class)
+public class PersistenceService implements IPersistenceService {
+ public static final String OSGI_JNDI_SERVICE_NAME = "osgi.jndi.service.name";
+ public static final String OSGI_JNDI_URL_PREFIX = "osgi:service/";
+ public static final String JAVAX_PERSISTENCE_PROVIDER_PROP = "javax.persistence.provider";
+ public static final String PERSISTENCE_PROVIDER = "javax.persistence.spi.PersistenceProvider";
+ public static final String ENTITYMANAGERFACTORY = "javax.persistence.EntityManagerFactory";
+ public static final String OSGI_UNIT_NAME = "osgi.unit.name";
+ private static final String FILTER_EMF = "(objectClass=javax.persistence.EntityManagerFactory)";
+ private static final String FILTER_EMF_WITH_PERSISTENCE = "(&(objectClass=javax.persistence.EntityManagerFactory)(osgi.unit.name=%s))";
+
+ private static final Logger log = LoggerFactory.getLogger(PersistenceService.class);
+ private IMondrianManager mondrianManager;
+
+ private BundleContext bundleContext = null;
+ private ServiceRegistration<?> serviceRegistration;
+ private Map<String, IDataSourceProperties> dsPropsMap = new HashMap<>();
+ private Map<String, CommonDataSource> dsMap = new HashMap<>();
+ private List<IPersistenceException> persistenceExceptionHandler = new ArrayList<>() ;
+ private ValidatorFactory validatorFactory;
+ private HashMap<String, EntityManagerFactory> emfactories = new HashMap<>();
+
+ protected void activate(ComponentContext context) {
+ if (log.isDebugEnabled())
+ log.debug("PersistenceService activated");
+ initialize(context);
+ }
+
+ protected void deactivate(ComponentContext context) { //NOSONAR
+ if (serviceRegistration != null) {
+ serviceRegistration.unregister();
+ } else {
+ if (log.isDebugEnabled())
+ log.debug("serviceRegistration not set, cannot unregister");
+ }
+ if (log.isDebugEnabled())
+ log.debug("PersistenceService deactivated");
+ }
+
+ @Reference(cardinality = ReferenceCardinality.MANDATORY, policy = ReferencePolicy.DYNAMIC)
+ void bindMondrianProvider(IMondrianManager mondrianManager) {
+ this.mondrianManager = mondrianManager;
+ }
+
+ void unbindMondrianProvider(IMondrianManager mondrianManager) { //NOSONAR
+ this.mondrianManager = null;
+ }
+
+ @Reference(cardinality = ReferenceCardinality.MANDATORY, policy = ReferencePolicy.DYNAMIC)
+ void bindValidationFactory(ValidatorFactory validatorFactory) {
+ this.validatorFactory = validatorFactory;
+ }
+
+ void unbindValidationFactory(ValidatorFactory validatorFactory) { //NOSONAR
+ this.validatorFactory = null;
+ }
+
+ public void initialize(ComponentContext context) {
+ if (log.isDebugEnabled())
+ log.debug("initializing PersistenceService");
+ bundleContext = context.getBundleContext();
+ // build properties map
+ createDataSourcePropertiesMap();
+ // read from jndi datasources from context or create them on the fly
+ // using preferred data sources
+ initDataSourceMap();
+ // register persistence provider in context
+ registerPersistenceProvider();
+ if (log.isDebugEnabled())
+ log.debug("PersistenceService ready");
+ }
+
+ public static void showPossibleConfigurationErrors(Logger logger) {
+ String[] messages = {
+ "Maybe misconfigured data sources? Verify configuration in Product properties for missing plugins or configuration!" };
+ for (String message : messages) {
+ logger.error(message);
+ }
+ }
+
+ private void createDataSourcePropertiesMap() {
+ for (String name : ProductConfiguration.getDataSourceNames()) {
+ IDataSourceProperties dsProp = new DataSourceProperties(name);
+ if (dsProp.isReasonable()) {
+ dsPropsMap.put(name, dsProp);
+ }
+ }
+ }
+
+ private DataSourceFactory lookupDsf(String clientDriverName) {
+ String filter = "(&(" + DataSourceFactory.OSGI_JDBC_DRIVER_CLASS + "=" + clientDriverName + "))";
+ if (log.isDebugEnabled())
+ log.debug("Filter is: " + filter);
+ return getDsf(filter);
+ }
+
+ @SuppressWarnings("unchecked")
+ private DataSourceFactory getDsf(String filter) {
+ ServiceReference<DataSourceFactory>[] refs = null;
+ try {
+ refs = (ServiceReference<DataSourceFactory>[]) bundleContext
+ .getServiceReferences(DataSourceFactory.class.getName(), filter);
+ } catch (InvalidSyntaxException isEx) {
+ throw new DataSourceFactoryException("Bad filter", isEx);
+ } catch (NullPointerException npe) {
+ showPossibleConfigurationErrors(log);
+ throw new DataSourceFactoryException(
+ "Maybe misconfigured data sources? Verify configuration in product properties for missing plugins or configuration!",
+ npe);
+ }
+ return (refs == null) ? null : bundleContext.getService(refs[0]);
+ }
+
+ private synchronized DataSource createDataSource(String jndiDatasource) {
+ DataSource ds = null;
+ try {
+ if (log.isDebugEnabled())
+ log.debug("createDataSource:" + jndiDatasource);
+ IDataSourceProperties dsProps = dsPropsMap.get(jndiDatasource);
+ DataSourceFactory dsf = null;
+ if (dsProps != null) {
+ dsf = lookupDsf(dsProps.getClientDriverName());
+ if (dsf == null) {
+ log.error("Driver {} for {} not registered", dsProps.getClientDriverName(), jndiDatasource);
+ return null;
+ }
+ ds = dsf.createDataSource(dsProps.getDataSourceProperties());
+ Hashtable<String, String> ht = new Hashtable<>(); // NOSONAR
+ ht.put(OSGI_JNDI_SERVICE_NAME, jndiDatasource);
+ bundleContext.registerService(DataSource.class, ds, ht);
+ }
+ } catch (Exception e) {
+ log.error("createDataSource '" + jndiDatasource + "' " + e.getClass().getCanonicalName(), e);
+ }
+ return ds;
+ }
+
+ private synchronized XADataSource createXADataSource(String jndiDatasource) {
+ XADataSource ds = null;
+ try {
+ if (log.isDebugEnabled())
+ log.debug("createXADataSource:" + jndiDatasource);
+ IDataSourceProperties dsProps = dsPropsMap.get(jndiDatasource);
+ DataSourceFactory dsf = null;
+ if (dsProps != null) {
+ dsf = lookupDsf(dsProps.getClientDriverName());
+ if (dsf == null) {
+ return null;
+ }
+ ds = dsf.createXADataSource(dsProps.getDataSourceProperties());
+ Hashtable<String, String> ht = new Hashtable<>(); // NOSONAR
+ ht.put(OSGI_JNDI_SERVICE_NAME, "XA" + jndiDatasource);
+ bundleContext.registerService(XADataSource.class, ds, ht);
+ }
+ } catch (Exception e) {
+ log.error("createXADataSource '" + jndiDatasource + "' " + e.getClass().getCanonicalName(), e);
+ }
+ return ds;
+ }
+
+ private DataSource lookupDS(String jndiDatasource) throws NamingException {
+ InitialContext ictx = new InitialContext();
+ return (DataSource) ictx.lookup(OSGI_JNDI_URL_PREFIX + jndiDatasource);
+ }
+
+ private XADataSource lookupXADS(String jndiDatasource) throws NamingException {
+ InitialContext ictx = new InitialContext();
+ return (XADataSource) ictx.lookup(OSGI_JNDI_URL_PREFIX + "XA" + jndiDatasource);
+ }
+
+ public void initDataSourceMap() {
+ if (log.isDebugEnabled())
+ log.debug("initDataSourceMap");
+ for (String jndiName : ProductConfiguration.getDataSourceNames()) {
+ CommonDataSource ds = getDataSource(jndiName);
+ if (ds != null) {
+ dsMap.put(jndiName, ds);
+ }
+ }
+ }
+
+ @Override
+ // must be checked if DataSource or XADatasource is returned
+ public CommonDataSource getDataSource(String jndiDatasource) {
+ if (log.isDebugEnabled())
+ log.debug("getDataSource:" + jndiDatasource);
+ if (dsMap.containsKey(jndiDatasource)) {
+ return dsMap.get(jndiDatasource);
+ }
+ DataSource ds = null;
+ try {
+ ds = lookupDS(jndiDatasource);
+ } catch (NamingException e) { // NOSONAR
+ } finally {
+ if (ds == null) {
+ ds = createDataSource(jndiDatasource);
+ }
+ }
+ if (ds != null) {
+ return ds;
+ }
+ XADataSource xads = null;
+ try {
+ xads = lookupXADS(jndiDatasource);
+ } catch (NamingException e) { // NOSONAR
+ } finally {
+ if (xads == null) {
+ xads = createXADataSource(jndiDatasource);
+ }
+ }
+ return xads;
+ }
+
+ @Override
+ public java.sql.Connection getJndiConnection(String jndiName) throws SQLException {
+ if (log.isDebugEnabled())
+ log.debug("getConnection:" + jndiName);
+ if (dsMap.get(jndiName) instanceof DataSource) {
+ return ((DataSource) dsMap.get(jndiName)).getConnection();
+ }
+ return null;
+ }
+
+ @Override
+ public java.sql.Connection getPersistenceUnitConnection(String persistenceUnit) throws SQLException {
+ return getJndiConnection(ProductConfiguration.getPersistenceUnitJndiName(persistenceUnit));
+ }
+
+ @Override
+ public XAConnection getXAJndiConnection(String jndiName) throws SQLException {
+ if (log.isDebugEnabled())
+ log.debug("getXAConnection:" + jndiName);
+ if (dsMap.get(jndiName) instanceof XADataSource) {
+ return ((XADataSource) dsMap.get(jndiName)).getXAConnection();
+ }
+ return null;
+ }
+
+ @Override
+ public XAConnection getXAPersistenceUnitConnection(String persistenceUnit) throws SQLException {
+ return getXAJndiConnection(ProductConfiguration.getPersistenceUnitJndiName(persistenceUnit));
+ }
+
+ @Override
+ public RolapConnection getMondrianConnection(String persistenceUnit, String cubePackageName) throws SQLException {
+ if (log.isDebugEnabled()) {
+ log.debug("mondrian connection:" + cubePackageName + " persistenceUnit:" + persistenceUnit);
+ }
+
+ IMondrianService service = mondrianManager.getService(cubePackageName);
+ if (service == null) {
+ log.error("No IMondrianCatalogService available for {}", cubePackageName);
+ return null;
+ }
+
+ PropertyList list = new PropertyList();
+ list.put("Provider", "mondrian");
+ list.put("Catalog", service.getMondrianCatalogURL().toString());
+ String jndiName = ProductConfiguration.getPersistenceUnitJndiName(persistenceUnit);
+ if (log.isDebugEnabled())
+ log.debug("mondrian connection jndi:" + jndiName);
+ return (RolapConnection) DriverManager.getConnection(list, null, (DataSource) getDataSource(jndiName));
+ }
+
+ @Override
+ public Result sendQuery(RolapConnection con, String query) {
+ return sendQuery(con, query, 10000);
+ }
+
+ @Override
+ public Result sendQuery(RolapConnection con, String query, long ms) {
+ log.debug("query: {}", query);
+ return con.execute(new Execution(con.parseQuery(query).getStatement(), ms));
+ }
+
+ @Override
+ public void clearCubeCache(RolapConnection connection, String cubeName) {
+ Cube cube = connection.getSchema().lookupCube(cubeName, true);
+ if(cube != null) {
+ CacheControl cacheControl = connection.getCacheControl(null);
+ CacheControl.CellRegion region = cacheControl.createMeasuresRegion(cube);
+ cacheControl.flush(region);
+ cacheControl.flushSchemaCache();
+ }
+ }
+
+ @Override
+ public void registerPersistenceUnit(String persistenceUnitName, Class<?> cls) {
+ // lookup entity manager factory
+ if (emfactories.containsKey(persistenceUnitName)) {
+ return;
+ }
+ EntityManagerFactory emf = createEntityManagerFactory(persistenceUnitName, cls);
+ if (emf != null) {
+ emfactories.put(persistenceUnitName, emf);
+ }
+ }
+
+ @Override
+ public EntityManagerFactory getEntityManagerFactory(String persistenceUnitName) {
+ if (emfactories.containsKey(persistenceUnitName)) {
+ return emfactories.get(persistenceUnitName);
+ }
+ log.error("an entitymanagerfactory with this persistence unit name is not registered: {}", persistenceUnitName);
+ return null;
+ }
+
+ private void registerPersistenceProvider() {
+ // register persistence provider
+ PersistenceProvider persistence = new PersistenceProvider();
+
+ Dictionary<String, String> props = new Hashtable<>(); // NOSONAR
+ props.put(JAVAX_PERSISTENCE_PROVIDER_PROP, persistence.getClass().getName());
+ bundleContext.registerService(PersistenceProvider.class.getName(), persistence, props);
+ // Required setting to be able to handle 'bundleresource' and 'bundleentry' protocol. DO NOT DELETE!
+ System.setProperty(SystemProperties.ARCHIVE_FACTORY, ArchiveFactoryImpl.class.getCanonicalName());
+ if (log.isDebugEnabled())
+ log.debug("persistence provider registered");
+ }
+
+ private EntityManagerFactory createEntityManagerFactory(String persistenceUnitName, Class<?> cls) {
+ // retrieve persistence provider from context
+ EntityManagerFactory emf = null;
+ ServiceReference<?>[] refs = null;
+ String filter = "(&(" + JAVAX_PERSISTENCE_PROVIDER_PROP + "=org.eclipse.persistence.jpa.PersistenceProvider))";
+ try {
+ refs = bundleContext.getAllServiceReferences(PersistenceProvider.class.getName(), filter);
+ } catch (InvalidSyntaxException e) {
+ log.error("exception while retrieving persistence providers", e);
+ }
+ if ((refs == null) || (refs.length < 1)) {
+ log.error(
+ "no persistence providers found, therefore the entity manager factory was NOT created and NOT registered for unit name {}",
+ persistenceUnitName);
+ } else {
+ // register all persistence units' emfs
+ Properties properties = null;
+ String jndiName = ProductConfiguration.getPersistenceUnitJndiName(persistenceUnitName);
+ CommonDataSource ds = getDataSource(jndiName);
+ properties = ProductConfiguration.getPersistenceUnitProperties(persistenceUnitName, ds,
+ cls.getClassLoader());
+ properties.put(PersistenceUnitProperties.DDL_GENERATION_INDEX_FOREIGN_KEYS, "true");
+ properties.put(PersistenceUnitProperties.VALIDATOR_FACTORY, validatorFactory);
+ properties.put(PersistenceUnitProperties.EXCEPTION_HANDLER_CLASS, "org.eclipse.osbp.persistence.fragment.PersistenceExceptionHandler");
+ PersistenceProvider persistenceProvider = (PersistenceProvider) bundleContext.getService(refs[0]);
+ if (log.isDebugEnabled())
+ log.debug("create entity manager factory for persistenceUnit " + persistenceUnitName + " jndi "
+ + jndiName + " with properties:" + properties.toString());
+ try {
+ emf = persistenceProvider.createEntityManagerFactory(persistenceUnitName, properties);
+ } catch (/* DatabaseException | PersistenceException | */Exception dbe) {
+ log.error("creating entity manager factory for " + persistenceUnitName, dbe);
+ }
+ if (emf != null) {
+ Session session = emf.unwrap(Session.class);
+ persistenceExceptionHandler.add((IPersistenceException) session.getExceptionHandler());
+ // register this factory -- attention: the same xa and non xa
+ // will replace each other
+ Dictionary<String, String> emfprops = new Hashtable<>(); //NOSONAR
+ emfprops.put(ENTITYMANAGERFACTORY, emf.getClass().getName());
+ emfprops.put(OSGI_UNIT_NAME, persistenceUnitName);
+ bundleContext.registerService(EntityManagerFactory.class.getName(), emf, emfprops);
+ if (log.isDebugEnabled())
+ log.debug("entity manager factory created and registered with unit name:" + persistenceUnitName);
+ } else {
+ log.error(
+ "incorrect persistence provider, therefore the entity manager factory was NOT created and NOT registered for unit name: {}",
+ persistenceUnitName);
+ }
+ }
+ return emf;
+ }
+
+ /**
+ * Create the filter to find the proper service.
+ *
+ * @return
+ * @throws InvalidSyntaxException
+ */
+ @Override
+ public Filter createEMFFilter(ComponentContext context, String persistenceUnitName) throws InvalidSyntaxException {
+ if (persistenceUnitName == null || persistenceUnitName.equals("")) {
+ return context.getBundleContext().createFilter(FILTER_EMF);
+ } else {
+ return context.getBundleContext()
+ .createFilter(String.format(FILTER_EMF_WITH_PERSISTENCE, persistenceUnitName));
+ }
+ }
+
+ @Override
+ public Map<String, EntityManagerFactory> getEntityManagerFactoryMap() {
+ return emfactories;
+ }
+
+ @Override
+ public void inheritEntityManagerFactoryMap(Map<String, EntityManagerFactory> entityManagerFactoryMap) {
+ for (Entry<String, EntityManagerFactory> persistenceUnit : entityManagerFactoryMap.entrySet()) {
+ emfactories.put(persistenceUnit.getKey(), persistenceUnit.getValue());
+ }
+ }
+
+ @Override
+ public void registerPersistenceExceptionNotification(IPersistenceException.Receiver receiver) {
+ persistenceExceptionHandler.forEach(it -> it.registerNotification(receiver));
+ }
+}
diff --git a/org.eclipse.osbp.persistence/src/org/eclipse/osbp/persistence/impl/Activator.java b/org.eclipse.osbp.persistence/src/org/eclipse/osbp/persistence/impl/Activator.java
deleted file mode 100644
index 224eb3b..0000000
--- a/org.eclipse.osbp.persistence/src/org/eclipse/osbp/persistence/impl/Activator.java
+++ /dev/null
@@ -1,44 +0,0 @@
-/**
- *
- * Copyright (c) 2011, 2016 - Loetz GmbH&Co.KG (69115 Heidelberg, Germany)
- *
- * All rights reserved. This program and the accompanying materials
- * are made available under the terms of the Eclipse Public License v1.0
- * which accompanies this distribution, and is available at
- * http://www.eclipse.org/legal/epl-v10.html
- *
- * Contributors:
- * Christophe Loetz (Loetz GmbH&Co.KG) - initial implementation
- */
-package org.eclipse.osbp.persistence.impl;
-
-import org.eclipse.osbp.gitinfo.Loginfo;
-import org.osgi.framework.BundleActivator;
-import org.osgi.framework.BundleContext;
-
-public class Activator implements BundleActivator {
-
- private static BundleContext context;
-
- static BundleContext getContext() {
- return context;
- }
-
- /*
- * (non-Javadoc)
- * @see org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext)
- */
- public void start(BundleContext bundleContext) throws Exception {
- Activator.context = bundleContext;
- new Loginfo().print( Activator.class.getCanonicalName(), Activator.class.getClassLoader());
- }
-
- /*
- * (non-Javadoc)
- * @see org.osgi.framework.BundleActivator#stop(org.osgi.framework.BundleContext)
- */
- public void stop(BundleContext bundleContext) throws Exception {
- Activator.context = null;
- }
-
-}
diff --git a/org.eclipse.osbp.persistence/src/org/eclipse/osbp/persistence/impl/PersistenceService.java b/org.eclipse.osbp.persistence/src/org/eclipse/osbp/persistence/impl/PersistenceService.java
deleted file mode 100644
index 90f5e1f..0000000
--- a/org.eclipse.osbp.persistence/src/org/eclipse/osbp/persistence/impl/PersistenceService.java
+++ /dev/null
@@ -1,503 +0,0 @@
-/**
- *
- * Copyright (c) 2011, 2016 - Loetz GmbH&Co.KG (69115 Heidelberg, Germany)
- *
- * All rights reserved. This program and the accompanying materials
- * are made available under the terms of the Eclipse Public License v1.0
- * which accompanies this distribution, and is available at
- * http://www.eclipse.org/legal/epl-v10.html
- *
- * Contributors:
- * Christophe Loetz (Loetz GmbH&Co.KG) - initial implementation
- *
- */
- package org.eclipse.osbp.persistence.impl;
-
-import java.io.PrintStream;
-import java.sql.SQLException;
-import java.util.Dictionary;
-import java.util.HashMap;
-import java.util.Hashtable;
-import java.util.Map;
-import java.util.Properties;
-
-import javax.naming.InitialContext;
-import javax.naming.NamingException;
-import javax.persistence.EntityManagerFactory;
-import javax.sql.CommonDataSource;
-import javax.sql.DataSource;
-import javax.sql.XAConnection;
-import javax.sql.XADataSource;
-import javax.validation.ValidatorFactory;
-
-import mondrian.olap.DriverManager;
-import mondrian.olap.Result;
-import mondrian.olap.Util.PropertyList;
-import mondrian.rolap.RolapConnection;
-import mondrian.server.Execution;
-
-import org.eclipse.osbp.mondrian.api.IMondrianManager;
-import org.eclipse.osbp.mondrian.api.IMondrianService;
-import org.eclipse.osbp.persistence.IDataSourceProperties;
-import org.eclipse.osbp.persistence.IPersistenceService;
-import org.eclipse.osbp.preferences.ProductConfiguration;
-import org.eclipse.persistence.config.PersistenceUnitProperties;
-import org.eclipse.persistence.config.SystemProperties;
-import org.eclipse.persistence.internal.jpa.deployment.ArchiveFactoryImpl;
-import org.eclipse.persistence.jpa.PersistenceProvider;
-import org.osgi.framework.BundleContext;
-import org.osgi.framework.Filter;
-import org.osgi.framework.InvalidSyntaxException;
-import org.osgi.framework.ServiceReference;
-import org.osgi.framework.ServiceRegistration;
-import org.osgi.service.component.ComponentContext;
-import org.osgi.service.component.annotations.Component;
-import org.osgi.service.component.annotations.Reference;
-import org.osgi.service.component.annotations.ReferenceCardinality;
-import org.osgi.service.component.annotations.ReferencePolicy;
-import org.osgi.service.jdbc.DataSourceFactory;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-@Component(service = IPersistenceService.class, immediate = true)
-public class PersistenceService implements IPersistenceService {
- public static final String OSGI_JNDI_SERVICE_NAME = "osgi.jndi.service.name";
- public static final String OSGI_JNDI_URL_PREFIX = "osgi:service/";
- public static final String JAVAX_PERSISTENCE_PROVIDER_PROP = "javax.persistence.provider";
- public static final String PERSISTENCE_PROVIDER = "javax.persistence.spi.PersistenceProvider";
- public static final String ENTITYMANAGERFACTORY = "javax.persistence.EntityManagerFactory";
- public static final String OSGI_UNIT_NAME = "osgi.unit.name";
- private static final String FILTER__EMF = "(objectClass=javax.persistence.EntityManagerFactory)";
- private static final String FILTER__EMF_WITH_PERSISTENCE = "(&(objectClass=javax.persistence.EntityManagerFactory)(osgi.unit.name=%s))";
-
- private final static Logger log = LoggerFactory
- .getLogger(PersistenceService.class);
-
- private ComponentContext ctx = null;
-
- private IMondrianManager mondrianManager;
-
- private BundleContext bundleContext = null;
- private ServiceRegistration<?> serviceRegistration;
- private Map<String, IDataSourceProperties> dsPropsMap = new HashMap<String, IDataSourceProperties>();
- private Map<String, CommonDataSource> dsMap = new HashMap<String, CommonDataSource>();
- private ValidatorFactory validatorFactory;
- private static ThreadLocal<HashMap<String, EntityManagerFactory>> threadLocal = new ThreadLocal<HashMap<String, EntityManagerFactory>>();
-
- public PersistenceService() {
- }
-
- protected void activate(ComponentContext context) {
- if (log.isDebugEnabled())
- log.debug("PersistenceService activated");
- this.ctx = context;
- initialize(ctx);
- }
-
- protected void deactivate(ComponentContext context) {
- if (serviceRegistration != null) {
- serviceRegistration.unregister();
- } else {
- if (log.isDebugEnabled())
- log.debug("serviceRegistration not set, cannot unregister");
- }
- if (log.isDebugEnabled())
- log.debug("PersistenceService deactivated");
- }
-
- @Reference(cardinality = ReferenceCardinality.MANDATORY, policy = ReferencePolicy.DYNAMIC)
- void bindMondrianProvider(IMondrianManager mondrianManager) {
- this.mondrianManager = mondrianManager;
- }
-
- void unbindMondrianProvider(IMondrianManager mondrianManager) {
- this.mondrianManager = null;
- }
-
- @Reference(cardinality = ReferenceCardinality.MANDATORY, policy = ReferencePolicy.DYNAMIC)
- void bindValidationFactory(ValidatorFactory validatorFactory) {
- this.validatorFactory = validatorFactory;
- }
-
- void unbindValidationFactory(ValidatorFactory validatorFactory) {
- this.validatorFactory = null;
- }
-
- public void initialize(ComponentContext context) {
- if (log.isDebugEnabled())
- log.debug("initializing PersistenceService");
- bundleContext = context.getBundleContext();
- // build properties map
- createDataSourcePropertiesMap();
- // read from jndi datasources from context or create them on the fly
- // using preferred data sources
- initDataSourceMap();
- // register persistence provider in context
- registerPersistenceProvider();
- if (log.isDebugEnabled())
- log.debug("PersistenceService ready");
- }
-
- public static void showPossibleConfigurationErrors(PrintStream print) {
- String[] messages = {
- "Maybe misconfigured data sources? Verify configuration in Product properties for missing plugins or configuration!"
- };
- for (String message : messages) {
- print.println(message);
- }
- }
-
- private void createDataSourcePropertiesMap() {
- for (String name : ProductConfiguration.getDataSourceNames()) {
- IDataSourceProperties dsProp = new DataSourceProperties(name);
- if (dsProp.isReasonable()) {
- dsPropsMap.put(name, dsProp);
- }
- }
- }
-
- private DataSourceFactory lookupDsf(String clientDriverName) {
- String filter = "(&(" + DataSourceFactory.OSGI_JDBC_DRIVER_CLASS + "="
- + clientDriverName + "))";
- if (log.isDebugEnabled())
- log.debug("Filter is: " + filter);
- return getDsf(filter);
- }
-
- @SuppressWarnings("unchecked")
- private DataSourceFactory getDsf(String filter) {
- ServiceReference<DataSourceFactory>[] refs = null;
- try {
- refs = (ServiceReference<DataSourceFactory>[]) bundleContext.getServiceReferences(
- DataSourceFactory.class.getName(), filter);
- } catch (InvalidSyntaxException isEx) {
- new RuntimeException("Bad filter", isEx);
- } catch (NullPointerException npe) {
- showPossibleConfigurationErrors(System.err);
- new RuntimeException("Maybe misconfigured data sources? Verify configuration in Product properties for missing plugins or configuration!", npe);
- }
- return (refs == null) ? null : (DataSourceFactory) bundleContext
- .getService(refs[0]);
- }
-
- private synchronized DataSource createDataSource(String jndiDatasource) {
- DataSource ds = null;
- try {
- if (log.isDebugEnabled())
- log.debug("createDataSource:" + jndiDatasource);
- IDataSourceProperties dsProps = dsPropsMap.get(jndiDatasource);
- DataSourceFactory dsf = null;
- if (dsProps != null) {
- dsf = lookupDsf(dsProps.getClientDriverName());
- }
- if (dsf == null) {
- log.error("DataSourceFactory for jndiDatasource ' "
- + jndiDatasource + " ' is null");
- return null;
- }
- ds = dsf.createDataSource(dsProps.getDataSourceProperties());
- Hashtable<String, String> ht = new Hashtable<String, String>();
- ht.put(OSGI_JNDI_SERVICE_NAME, jndiDatasource);
- bundleContext.registerService(DataSource.class, ds, ht);
- } catch (Exception e) {
- log.error("createDataSource '" + jndiDatasource + "' "
- + e.getClass().getCanonicalName(), e);
- }
- return ds;
- }
-
- private synchronized XADataSource createXADataSource(String jndiDatasource) {
- XADataSource ds = null;
- try {
- if (log.isDebugEnabled())
- log.debug("createXADataSource:" + jndiDatasource);
- IDataSourceProperties dsProps = dsPropsMap.get(jndiDatasource);
- DataSourceFactory dsf = null;
- if (dsProps != null) {
- dsf = lookupDsf(dsProps.getClientDriverName());
- }
- if (dsf == null) {
- log.error("DataSourceFactory for jndiDatasource ' "
- + jndiDatasource + " ' is null");
- return null;
- }
- ds = dsf.createXADataSource(dsProps.getDataSourceProperties());
- Hashtable<String, String> ht = new Hashtable<String, String>();
- ht.put(OSGI_JNDI_SERVICE_NAME, "XA" + jndiDatasource);
- bundleContext.registerService(XADataSource.class, ds, ht);
- } catch (Exception e) {
- log.error("createXADataSource '" + jndiDatasource + "' "
- + e.getClass().getCanonicalName(), e);
- }
- return ds;
- }
-
- private DataSource lookupDS(String jndiDatasource) throws NamingException {
- InitialContext ictx = new InitialContext();
- return (DataSource) ictx.lookup(OSGI_JNDI_URL_PREFIX + jndiDatasource);
- }
-
- private XADataSource lookupXADS(String jndiDatasource)
- throws NamingException {
- InitialContext ictx = new InitialContext();
- return (XADataSource) ictx.lookup(OSGI_JNDI_URL_PREFIX + "XA"
- + jndiDatasource);
- }
-
- public void initDataSourceMap() {
- if (log.isDebugEnabled())
- log.debug("initDataSourceMap");
- for (String jndiName : ProductConfiguration.getDataSourceNames()) {
- CommonDataSource ds = getDataSource(jndiName);
- if (ds != null) {
- dsMap.put(jndiName, ds);
- }
- }
- }
-
- @Override
- // must be checked if DataSource or XADatasource is returned
- public CommonDataSource getDataSource(String jndiDatasource) {
- if (log.isDebugEnabled())
- log.debug("getDataSource:" + jndiDatasource);
- if (dsMap.containsKey(jndiDatasource)) {
- return dsMap.get(jndiDatasource);
- }
- DataSource ds = null;
- try {
- ds = lookupDS(jndiDatasource);
- } catch (NamingException e) {
- } finally {
- if (ds == null) {
- ds = createDataSource(jndiDatasource);
- }
- }
- if (ds != null) {
- return ds;
- }
- XADataSource xads = null;
- try {
- xads = lookupXADS(jndiDatasource);
- } catch (NamingException e) {
- } finally {
- if (xads == null) {
- xads = createXADataSource(jndiDatasource);
- }
- }
- return xads;
- }
-
- @Override
- public java.sql.Connection getJndiConnection(String jndiName)
- throws SQLException {
- if (log.isDebugEnabled())
- log.debug("getConnection:" + jndiName);
- if (dsMap.get(jndiName) instanceof DataSource) {
- return ((DataSource) dsMap.get(jndiName)).getConnection();
- }
- return null;
- }
-
- @Override
- public java.sql.Connection getPersistenceUnitConnection(
- String persistenceUnit) throws SQLException {
- return getJndiConnection(ProductConfiguration
- .getPersistenceUnitJndiName(persistenceUnit));
- }
-
- @Override
- public XAConnection getXAJndiConnection(String jndiName)
- throws SQLException {
- if (log.isDebugEnabled())
- log.debug("getXAConnection:" + jndiName);
- if (dsMap.get(jndiName) instanceof XADataSource) {
- return ((XADataSource) dsMap.get(jndiName)).getXAConnection();
- }
- return null;
- }
-
- @Override
- public XAConnection getXAPersistenceUnitConnection(String persistenceUnit)
- throws SQLException {
- return getXAJndiConnection(ProductConfiguration
- .getPersistenceUnitJndiName(persistenceUnit));
- }
-
- @Override
- public RolapConnection getMondrianConnection(String persistenceUnit,
- String cubePackageName) throws SQLException {
- if (log.isDebugEnabled()) {
- log.debug("mondrian connection:" + cubePackageName
- + " persistenceUnit:" + persistenceUnit);
- }
-
- IMondrianService service = mondrianManager
- .getService(cubePackageName);
- if (service == null) {
- log.error("No IMondrianCatalogService available for "
- + cubePackageName);
- return null;
- }
-
- PropertyList list = new PropertyList();
- list.put("Provider", "mondrian");
- list.put("Catalog", service.getMondrianCatalogURL().toString());
- String jndiName = ProductConfiguration
- .getPersistenceUnitJndiName(persistenceUnit);
- if (log.isDebugEnabled())
- log.debug("mondrian connection jndi:" + jndiName);
- return (RolapConnection) DriverManager.getConnection(list, null,
- (DataSource) getDataSource(jndiName));
- }
-
- @Override
- public Result sendQuery(RolapConnection con, String query) {
- return sendQuery(con, query, 10000);
- }
-
- @Override
- public Result sendQuery(RolapConnection con, String query, long ms) {
- log.debug("query: " + query);
- return (Result) con.execute(new Execution(con.parseQuery(query)
- .getStatement(), ms));
- }
-
- @Override
- public void registerPersistenceUnit(String persistenceUnitName, Class<?> cls) {
- if (threadLocal.get() == null) {
- threadLocal.set(new HashMap<String, EntityManagerFactory>());
- }
- // lookup entity manager factory
- if (threadLocal.get().containsKey(persistenceUnitName)) {
- return;
- }
- EntityManagerFactory emf = createEntityManagerFactory(
- persistenceUnitName, cls);
- if (emf != null) {
- threadLocal.get().put(persistenceUnitName, emf);
- }
- }
-
- @Override
- public EntityManagerFactory getEntityManagerFactory(
- String persistenceUnitName) {
- if (threadLocal.get() == null) {
- threadLocal.set(new HashMap<String, EntityManagerFactory>());
- }
- if (threadLocal.get().containsKey(persistenceUnitName)) {
- return threadLocal.get().get(persistenceUnitName);
- }
- log.error("an entitymanagerfactory with this persistence unit name is not registered:"
- + persistenceUnitName);
- return null;
- }
-
- private void registerPersistenceProvider() {
- // register persistence provider
- PersistenceProvider persistence = new PersistenceProvider();
-
- Dictionary<String, String> props = new Hashtable<String, String>();
- props.put(JAVAX_PERSISTENCE_PROVIDER_PROP, persistence.getClass()
- .getName());
- bundleContext.registerService(PersistenceProvider.class.getName(),
- persistence, props);
- // Required setting to be able to handle 'bundleresource' and
- // 'bundleentry' protocol. DO NOT DELETE!!!!!!!!!!!
- System.setProperty(SystemProperties.ARCHIVE_FACTORY,
- ArchiveFactoryImpl.class.getCanonicalName());
- if (log.isDebugEnabled())
- log.debug("persistence provider registered");
- }
-
- private EntityManagerFactory createEntityManagerFactory(
- String persistenceUnitName, Class<?> cls) {
- // retrieve persistence provider from context
- EntityManagerFactory emf = null;
- ServiceReference<?>[] refs = null;
- String filter = "(&(" + JAVAX_PERSISTENCE_PROVIDER_PROP
- + "=org.eclipse.persistence.jpa.PersistenceProvider))";
- try {
- refs = bundleContext.getAllServiceReferences(
- PersistenceProvider.class.getName(), filter);
- } catch (InvalidSyntaxException e) {
- log.error("exception while retrieving persistence providers", e);
- }
- if ((refs == null) || (refs.length < 1)) {
- log.error("no persistence providers found, therefore the entity manager factory was NOT created and NOT registered for unit name:"
- + persistenceUnitName);
- } else {
- // register all persistence units' emfs
- Properties properties = null;
- String jndiName = ProductConfiguration
- .getPersistenceUnitJndiName(persistenceUnitName);
- CommonDataSource ds = getDataSource(jndiName);
- properties = ProductConfiguration.getPersistenceUnitProperties(
- persistenceUnitName, ds, cls.getClassLoader());
- properties.put(PersistenceUnitProperties.VALIDATOR_FACTORY, validatorFactory);
- // properties.put(PersistenceUnitProperties.SESSION_CUSTOMIZER,
- // "org.eclipse.osbp.datasources.CCSessionCustomizer");
- PersistenceProvider persistenceProvider = (PersistenceProvider) bundleContext
- .getService(refs[0]);
- if (log.isDebugEnabled())
- log.debug("create entity manager factory for persistenceUnit "
- + persistenceUnitName + " jndi " + jndiName
- + " with properties:" + properties.toString());
- try {
- emf = persistenceProvider.createEntityManagerFactory(
- persistenceUnitName, properties);
- } catch (/* DatabaseException | PersistenceException | */Exception dbe) {
- log.error("creating entity manager factory for "
- + persistenceUnitName, dbe);
- }
- if (emf != null) {
- // register this factory -- attention: the same xa and non xa
- // will replace each other
- Dictionary<String, String> emfprops = new Hashtable<String, String>();
- emfprops.put(ENTITYMANAGERFACTORY, emf.getClass().getName());
- emfprops.put(OSGI_UNIT_NAME, persistenceUnitName);
- bundleContext.registerService(
- EntityManagerFactory.class.getName(), emf, emfprops);
- if (log.isDebugEnabled())
- log.debug("entity manager factory created and registered with unit name:"
- + persistenceUnitName);
- } else {
- log.error("incorrect persistence provider, therefore the entity manager factory was NOT created and NOT registered for unit name:"
- + persistenceUnitName);
- }
- }
- return emf;
- }
-
- /**
- * Create the filter to find the proper service.
- *
- * @return
- * @throws InvalidSyntaxException
- */
- @Override
- public Filter createEMFFilter(ComponentContext context,
- String persistenceUnitName) throws InvalidSyntaxException {
- if (persistenceUnitName == null || persistenceUnitName.equals("")) {
- return context.getBundleContext().createFilter(FILTER__EMF);
- } else {
- return context.getBundleContext().createFilter(
- String.format(FILTER__EMF_WITH_PERSISTENCE,
- persistenceUnitName));
- }
- }
-
- @Override
- public Map<String, EntityManagerFactory> getEntityManagerFactoryMap() {
- return threadLocal.get();
- }
-
- @Override
- public void inheritEntityManagerFactoryMap(Map<String, EntityManagerFactory> entityManagerFactoryMap) {
- if (threadLocal.get() == null) {
- threadLocal.set(new HashMap<String, EntityManagerFactory>());
- for (String persistenceUnitName : entityManagerFactoryMap.keySet()) {
- threadLocal.get().put(persistenceUnitName, entityManagerFactoryMap.get(persistenceUnitName));
- }
- }
- }
-}