From 32591a5994b1928d1631767ec615ba5377fc3f39 Mon Sep 17 00:00:00 2001 From: Roberto E. Escobar Date: Tue, 9 Dec 2014 17:22:16 -0700 Subject: feature[ats_ATS144309]: Add ClientJdbcService Change-Id: I4eb8826d81154843c69a7c17b3ce161006be8ad9 --- .../META-INF/MANIFEST.MF | 1 + .../OSGI-INF/client.jdbc.service.xml | 11 ++ .../client/internal/ClientJdbcServiceImpl.java | 149 +++++++++++++++++++++ 3 files changed, 161 insertions(+) create mode 100644 plugins/org.eclipse.osee.framework.core.client/OSGI-INF/client.jdbc.service.xml create mode 100644 plugins/org.eclipse.osee.framework.core.client/src/org/eclipse/osee/framework/core/client/internal/ClientJdbcServiceImpl.java (limited to 'plugins') diff --git a/plugins/org.eclipse.osee.framework.core.client/META-INF/MANIFEST.MF b/plugins/org.eclipse.osee.framework.core.client/META-INF/MANIFEST.MF index 057ddc0a4d1..a217caff6f3 100644 --- a/plugins/org.eclipse.osee.framework.core.client/META-INF/MANIFEST.MF +++ b/plugins/org.eclipse.osee.framework.core.client/META-INF/MANIFEST.MF @@ -19,6 +19,7 @@ Import-Package: org.eclipse.core.runtime, org.eclipse.osee.framework.logging, org.eclipse.osee.framework.plugin.core, org.eclipse.osee.framework.plugin.core.util, + org.eclipse.osee.jdbc, org.osgi.framework Export-Package: org.eclipse.osee.framework.core.client, org.eclipse.osee.framework.core.client.server diff --git a/plugins/org.eclipse.osee.framework.core.client/OSGI-INF/client.jdbc.service.xml b/plugins/org.eclipse.osee.framework.core.client/OSGI-INF/client.jdbc.service.xml new file mode 100644 index 00000000000..d6528daea93 --- /dev/null +++ b/plugins/org.eclipse.osee.framework.core.client/OSGI-INF/client.jdbc.service.xml @@ -0,0 +1,11 @@ + + + + + + + +skynet.jdbc.service +activity.jdbc.service + + diff --git a/plugins/org.eclipse.osee.framework.core.client/src/org/eclipse/osee/framework/core/client/internal/ClientJdbcServiceImpl.java b/plugins/org.eclipse.osee.framework.core.client/src/org/eclipse/osee/framework/core/client/internal/ClientJdbcServiceImpl.java new file mode 100644 index 00000000000..fbb9f4dcef8 --- /dev/null +++ b/plugins/org.eclipse.osee.framework.core.client/src/org/eclipse/osee/framework/core/client/internal/ClientJdbcServiceImpl.java @@ -0,0 +1,149 @@ +/******************************************************************************* + * Copyright (c) 2014 Boeing. + * 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: + * Boeing - initial API and implementation + *******************************************************************************/ +package org.eclipse.osee.framework.core.client.internal; + +import java.lang.reflect.InvocationHandler; +import java.lang.reflect.Method; +import java.lang.reflect.Proxy; +import java.util.LinkedHashSet; +import java.util.Map; +import java.util.Map.Entry; +import java.util.Properties; +import java.util.Set; +import java.util.concurrent.atomic.AtomicReference; +import org.eclipse.osee.framework.core.data.IDatabaseInfo; +import org.eclipse.osee.framework.core.data.OseeSessionGrant; +import org.eclipse.osee.jdbc.JdbcClient; +import org.eclipse.osee.jdbc.JdbcClientBuilder; +import org.eclipse.osee.jdbc.JdbcConstants; +import org.eclipse.osee.jdbc.JdbcServerConfig; +import org.eclipse.osee.jdbc.JdbcService; + +/** + * Temporary Service implementation until client is dynamically configured using ConfigAdmin + * + * @author Roberto E. Escobar + */ +public class ClientJdbcServiceImpl implements JdbcService { + + private final AtomicReference clientRef = new AtomicReference(); + + private final JdbcClient clientProxy = createClientProxy(); + + private String id = "N/A"; + private Set bindings; + + public void start(Map props) { + bindings = new LinkedHashSet(); + String[] values = getBindings(props); + for (String value : values) { + bindings.add(value); + } + synchronized (clientRef) { + InternalClientSessionManager instance = InternalClientSessionManager.getInstance(); + OseeSessionGrant sessionGrant = instance.getOseeSessionGrant(); + IDatabaseInfo dbInfo = sessionGrant.getDatabaseInfo(); + + JdbcClient jdbcClient = newClient(dbInfo); + clientRef.set(jdbcClient); + + id = dbInfo.getId(); + } + } + + private String[] getBindings(Map props) { + String[] toReturn = new String[0]; + if (props != null && !props.isEmpty()) { + Object binding = props.get(JdbcConstants.JDBC_SERVICE__OSGI_BINDING); + if (binding instanceof String) { + toReturn = new String[] {(String) binding}; + } else if (binding instanceof String[]) { + toReturn = (String[]) binding; + } + } + return toReturn; + } + + public void stop(Map props) { + bindings = null; + } + + private JdbcClient newClient(IDatabaseInfo dbInfo) { + JdbcClientBuilder builder = JdbcClientBuilder.newBuilder()// + .dbDriver(dbInfo.getDriver())// + .dbUri(dbInfo.getConnectionUrl())// + .dbUsername(dbInfo.getDatabaseLoginName())// + .production(dbInfo.isProduction()); + + Properties properties = dbInfo.getConnectionProperties(); + if (properties != null && !properties.isEmpty()) { + for (Entry entry : properties.entrySet()) { + builder.dbParam((String) entry.getKey(), (String) entry.getValue()); + } + } + return builder.build(); + } + + @Override + public String getId() { + return id; + } + + @Override + public JdbcClient getClient() { + return clientProxy; + } + + @Override + public Set getBindings() { + return bindings; + } + + @Override + public boolean hasServer() { + return false; + } + + @Override + public JdbcServerConfig getServerConfig() { + return null; + } + + @Override + public boolean isServerAlive(long waitTime) { + return false; + } + + private JdbcClient createClientProxy() { + InvocationHandler handler = new JdbcClientInvocationHandler(); + Class[] types = new Class[] {JdbcClient.class}; + return (JdbcClient) Proxy.newProxyInstance(this.getClass().getClassLoader(), types, handler); + } + + private final class JdbcClientInvocationHandler implements InvocationHandler { + + @Override + public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { + try { + JdbcClient client = clientRef.get(); + return method.invoke(client, args); + } catch (Throwable ex) { + Throwable cause = ex.getCause(); + if (cause == null) { + cause = ex; + } + throw cause; + } + } + + } + +} \ No newline at end of file -- cgit v1.2.3