This is an automated email from the ASF dual-hosted git repository. cdutz pushed a commit to branch resync-github in repository https://gitbox.apache.org/repos/asf/incubator-plc4x.git
commit 1be5d8dd628ccdf62fe59264b80e906f8d8cff75 Author: Christofer Dutz <christofer.d...@c-ware.de> AuthorDate: Fri Oct 26 13:46:09 2018 +0200 - Manually reverted the difference between github and gitbox master in order to re-sync the repos. --- .../org/apache/plc4x/java/PlcDriverManager.java | 8 +- .../plc4x/java/PlcDriverManagerInterface.java | 29 ---- .../plc4x/java/connectionpool/ConnectionProxy.java | 24 ---- .../java/connectionpool/PlcConnectionManager.java | 154 --------------------- .../java/connectionpool/PlcConnectionProxy.java | 101 -------------- .../exceptions/NoConnectionAvailableException.java | 30 ---- .../java/exceptions/NotConnectedException.java | 34 ----- .../connectionpool/PlcConnectionManagerTest.java | 99 ------------- .../connectionpool/PlcConnectionProxyTest.java | 74 ---------- 9 files changed, 3 insertions(+), 550 deletions(-) diff --git a/plc4j/core/src/main/java/org/apache/plc4x/java/PlcDriverManager.java b/plc4j/core/src/main/java/org/apache/plc4x/java/PlcDriverManager.java index 1699df8..1a433c7 100644 --- a/plc4j/core/src/main/java/org/apache/plc4x/java/PlcDriverManager.java +++ b/plc4j/core/src/main/java/org/apache/plc4x/java/PlcDriverManager.java @@ -18,10 +18,10 @@ under the License. */ package org.apache.plc4x.java; -import org.apache.plc4x.java.spi.PlcDriver; -import org.apache.plc4x.java.api.authentication.PlcAuthentication; import org.apache.plc4x.java.api.PlcConnection; +import org.apache.plc4x.java.api.authentication.PlcAuthentication; import org.apache.plc4x.java.api.exceptions.PlcConnectionException; +import org.apache.plc4x.java.spi.PlcDriver; import java.net.URI; import java.net.URISyntaxException; @@ -29,7 +29,7 @@ import java.util.HashMap; import java.util.Map; import java.util.ServiceLoader; -public class PlcDriverManager implements PlcDriverManagerInterface { +public class PlcDriverManager { protected ClassLoader classLoader; @@ -60,7 +60,6 @@ public class PlcDriverManager implements PlcDriverManagerInterface { * @return PlcConnection object. * @throws PlcConnectionException an exception if the connection attempt failed. */ - @Override public PlcConnection getConnection(String url) throws PlcConnectionException { PlcDriver driver = getDriver(url); PlcConnection connection = driver.connect(url); @@ -76,7 +75,6 @@ public class PlcDriverManager implements PlcDriverManagerInterface { * @return PlcConnection object. * @throws PlcConnectionException an exception if the connection attempt failed. */ - @Override public PlcConnection getConnection(String url, PlcAuthentication authentication) throws PlcConnectionException { PlcDriver driver = getDriver(url); PlcConnection connection = driver.connect(url, authentication); diff --git a/plc4j/core/src/main/java/org/apache/plc4x/java/PlcDriverManagerInterface.java b/plc4j/core/src/main/java/org/apache/plc4x/java/PlcDriverManagerInterface.java deleted file mode 100644 index c9b8ef2..0000000 --- a/plc4j/core/src/main/java/org/apache/plc4x/java/PlcDriverManagerInterface.java +++ /dev/null @@ -1,29 +0,0 @@ -/* -Licensed to the Apache Software Foundation (ASF) under one -or more contributor license agreements. See the NOTICE file -distributed with this work for additional information -regarding copyright ownership. The ASF licenses this file -to you under the Apache License, Version 2.0 (the -"License"); you may not use this file except in compliance -with the License. You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, -software distributed under the License is distributed on an -"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -KIND, either express or implied. See the License for the -specific language governing permissions and limitations -under the License. -*/ -package org.apache.plc4x.java; - -import org.apache.plc4x.java.api.authentication.PlcAuthentication; -import org.apache.plc4x.java.api.connection.PlcConnection; -import org.apache.plc4x.java.api.exceptions.PlcConnectionException; - -public interface PlcDriverManagerInterface { - PlcConnection getConnection(String url) throws PlcConnectionException; - - PlcConnection getConnection(String url, PlcAuthentication authentication) throws PlcConnectionException; -} diff --git a/plc4j/core/src/main/java/org/apache/plc4x/java/connectionpool/ConnectionProxy.java b/plc4j/core/src/main/java/org/apache/plc4x/java/connectionpool/ConnectionProxy.java deleted file mode 100644 index b1188d5..0000000 --- a/plc4j/core/src/main/java/org/apache/plc4x/java/connectionpool/ConnectionProxy.java +++ /dev/null @@ -1,24 +0,0 @@ -/* -Licensed to the Apache Software Foundation (ASF) under one -or more contributor license agreements. See the NOTICE file -distributed with this work for additional information -regarding copyright ownership. The ASF licenses this file -to you under the Apache License, Version 2.0 (the -"License"); you may not use this file except in compliance -with the License. You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, -software distributed under the License is distributed on an -"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -KIND, either express or implied. See the License for the -specific language governing permissions and limitations -under the License. -*/ -package org.apache.plc4x.java.connectionpool; - -import org.apache.plc4x.java.api.connection.PlcConnection; - -public interface ConnectionProxy extends PlcConnection { -} \ No newline at end of file diff --git a/plc4j/core/src/main/java/org/apache/plc4x/java/connectionpool/PlcConnectionManager.java b/plc4j/core/src/main/java/org/apache/plc4x/java/connectionpool/PlcConnectionManager.java deleted file mode 100644 index fc3ff01..0000000 --- a/plc4j/core/src/main/java/org/apache/plc4x/java/connectionpool/PlcConnectionManager.java +++ /dev/null @@ -1,154 +0,0 @@ -/* -Licensed to the Apache Software Foundation (ASF) under one -or more contributor license agreements. See the NOTICE file -distributed with this work for additional information -regarding copyright ownership. The ASF licenses this file -to you under the Apache License, Version 2.0 (the -"License"); you may not use this file except in compliance -with the License. You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, -software distributed under the License is distributed on an -"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -KIND, either express or implied. See the License for the -specific language governing permissions and limitations -under the License. -*/ -package org.apache.plc4x.java.connectionpool; - -import org.apache.plc4x.java.PlcDriverManager; -import org.apache.plc4x.java.PlcDriverManagerInterface; -import org.apache.plc4x.java.api.authentication.PlcAuthentication; -import org.apache.plc4x.java.api.connection.PlcConnection; -import org.apache.plc4x.java.api.exceptions.PlcConnectionException; -import org.apache.plc4x.java.exceptions.NoConnectionAvailableException; -import org.apache.plc4x.java.exceptions.NotConnectedException; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import javax.annotation.PreDestroy; -import java.util.HashMap; -import java.util.Map; - - -/** - * Plc Connection Manager to act as a controller to give threads access to a connection in the pool only if it is not - * used by a different thread - */ -public class PlcConnectionManager implements PlcDriverManagerInterface { - private static final Logger logger = LoggerFactory.getLogger(PlcConnectionManager.class); - //TODO: implement connection Pool with more than one connection - private Map<String, PlcConnection> connections = new HashMap<>(); - private Map<String, Boolean> isBlockedMap = new HashMap<>(); - private PlcDriverManager plcDriverManager; - - - public PlcConnectionManager(Map<String, PlcConnection> connectionProxyMap, Map<String, Boolean> isBlockedMap) { - this.connections = connectionProxyMap; - this.isBlockedMap = isBlockedMap; - plcDriverManager = new PlcDriverManager(); - } - - - /** - * Connects to a PLC using the given plc connection string when no connection was requested jet and builds the pool. - * Else it return the connection from the pool in a connection proxy. - * - * @param connectionString plc url with protocol prefix to select correct connection - * @return PlcConnection in a proxy - * @throws PlcConnectionException if connection was unsuccessfull, or the connection all other connections are - * being used by different threads - */ - @Override - public PlcConnection getConnection(String connectionString) throws PlcConnectionException { - if (!connections.containsKey(connectionString)) { - connections.put(connectionString,plcDriverManager.getConnection(connectionString)); - isBlockedMap.put(connectionString, false); - } - if (connections.containsKey(connectionString) && isBlockedMap.containsKey(connectionString)) { - // TODO: await set time till connection maybe available - if (!isBlockedMap.get(connectionString)) { - return tryConnect(connectionString); - } else { - throw new NoConnectionAvailableException("Connection not available"); - } - } else { - throw new NotConnectedException("Connection was not possible in the first place"); - } - } - - /** - * Connects to a PLC using the given plc connection string and authentification when no connection was requested jet and builds the pool. - * Else it return the connection from the pool in a connection proxy. - * - * @param connectionString plc url with protocol prefix to select correct connection - * @param authentication authentication credentials. - * @return PlcConnection in a proxy - * @throws PlcConnectionException if connection was unsuccessfull, or the connection all other connections are - * being used by different threads - */ - @Override - public PlcConnection getConnection(String connectionString, PlcAuthentication authentication) throws PlcConnectionException { - if (!connections.containsKey(connectionString)) { - connections.put(connectionString,plcDriverManager.getConnection(connectionString,authentication)); - isBlockedMap.put(connectionString, false); - } - if (connections.containsKey(connectionString) && isBlockedMap.containsKey(connectionString)) { - // TODO: await set time till connection maybe available - if (!isBlockedMap.get(connectionString)) { - return tryConnect(connectionString); - } else { - throw new NoConnectionAvailableException("Connection not available"); - } - } else { - throw new NotConnectedException("Connection was not possible in the first place"); - } - } - - /** - * helper function to check if the connection can be established if it is not already established - * and get the available connection from map and return a proxy that tells this manager when the connection is closed - * - * @param connectionString of source - * @return PlcConnection to return in getConnection - * @throws NotConnectedException if connection could not be established - */ - private PlcConnectionProxy tryConnect(String connectionString) throws NotConnectedException { - try { - if (connections.get(connectionString).isConnected()) { - isBlockedMap.put(connectionString, true); - return new PlcConnectionProxy(this, connectionString, connections.get(connectionString)); - } else { - connections.get(connectionString).connect(); - isBlockedMap.put(connectionString, true); - return new PlcConnectionProxy(this, connectionString, connections.get(connectionString)); - } - } catch (PlcConnectionException ex) { - throw new NotConnectedException(ex); - } - } - - /** - * After using the connection received from getReader or getConnection to release the connection again - * @param connectionString for which the connection should be released again - */ - public void returnConnection(String connectionString) { - isBlockedMap.put(connectionString, false); - } - - /** - * close all connections before closing - */ - @PreDestroy - public void close() { - for (PlcConnection plcConnection : connections.values()) { - try { - plcConnection.close(); - } catch (Exception ex) { - logger.error("Error disconnecting", ex); - } - } - } -} diff --git a/plc4j/core/src/main/java/org/apache/plc4x/java/connectionpool/PlcConnectionProxy.java b/plc4j/core/src/main/java/org/apache/plc4x/java/connectionpool/PlcConnectionProxy.java deleted file mode 100644 index ea9884f..0000000 --- a/plc4j/core/src/main/java/org/apache/plc4x/java/connectionpool/PlcConnectionProxy.java +++ /dev/null @@ -1,101 +0,0 @@ -/* -Licensed to the Apache Software Foundation (ASF) under one -or more contributor license agreements. See the NOTICE file -distributed with this work for additional information -regarding copyright ownership. The ASF licenses this file -to you under the Apache License, Version 2.0 (the -"License"); you may not use this file except in compliance -with the License. You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, -software distributed under the License is distributed on an -"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -KIND, either express or implied. See the License for the -specific language governing permissions and limitations -under the License. -*/ -package org.apache.plc4x.java.connectionpool; - - -import org.apache.plc4x.java.api.connection.PlcConnection; -import org.apache.plc4x.java.api.connection.PlcReader; -import org.apache.plc4x.java.api.connection.PlcSubscriber; -import org.apache.plc4x.java.api.connection.PlcWriter; - -import java.util.Optional; - -/** - * wrapper for PlcConnections to make access thread safe - * Tells ConnectionManager when the connection is not used anymore - */ -public class PlcConnectionProxy implements PlcConnection { - private final PlcConnectionManager parent; - private final String connectionString; - private final PlcConnection plcConnection; - private boolean closed; - - - public PlcConnectionProxy(PlcConnectionManager parent, String connectionString, PlcConnection plcConnection) { - this.parent = parent; - this.connectionString = connectionString; - this.plcConnection = plcConnection; - closed = false; - } - - /** - * connect should already be handled in the connection manager - * returns without doing anything - */ - @Override - public void connect() { - } - - @Override - public boolean isConnected() { - if(closed){ - return false; - }else { - return plcConnection.isConnected(); - } - } - - /** - * tell PlcConnectionManager that the connection is free again - */ - @Override - public void close() { - closed = true; - parent.returnConnection(connectionString); - } - - @Override - public Optional<PlcReader> getReader() { - if(closed){ - return Optional.empty(); - }else { - return plcConnection.getReader(); - } - } - - @Override - public Optional<PlcWriter> getWriter() { - if(closed) { - return Optional.empty(); - }else{ - return plcConnection.getWriter(); - } - } - - @Override - public Optional<PlcSubscriber> getSubscriber() { - if(closed){ - return Optional.empty(); - }else { - return plcConnection.getSubscriber(); - } - } - - -} diff --git a/plc4j/core/src/main/java/org/apache/plc4x/java/exceptions/NoConnectionAvailableException.java b/plc4j/core/src/main/java/org/apache/plc4x/java/exceptions/NoConnectionAvailableException.java deleted file mode 100644 index 9ae94b5..0000000 --- a/plc4j/core/src/main/java/org/apache/plc4x/java/exceptions/NoConnectionAvailableException.java +++ /dev/null @@ -1,30 +0,0 @@ -/* -Licensed to the Apache Software Foundation (ASF) under one -or more contributor license agreements. See the NOTICE file -distributed with this work for additional information -regarding copyright ownership. The ASF licenses this file -to you under the Apache License, Version 2.0 (the -"License"); you may not use this file except in compliance -with the License. You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, -software distributed under the License is distributed on an -"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -KIND, either express or implied. See the License for the -specific language governing permissions and limitations -under the License. -*/ -package org.apache.plc4x.java.exceptions; - -import org.apache.plc4x.java.api.exceptions.PlcConnectionException; - -/** - * Exception that should be thrown if the connection is used by a different thread and not realeased in time - */ -public class NoConnectionAvailableException extends PlcConnectionException { - public NoConnectionAvailableException(String msg) { - super(msg); - } -} diff --git a/plc4j/core/src/main/java/org/apache/plc4x/java/exceptions/NotConnectedException.java b/plc4j/core/src/main/java/org/apache/plc4x/java/exceptions/NotConnectedException.java deleted file mode 100644 index b1673d8..0000000 --- a/plc4j/core/src/main/java/org/apache/plc4x/java/exceptions/NotConnectedException.java +++ /dev/null @@ -1,34 +0,0 @@ -/* -Licensed to the Apache Software Foundation (ASF) under one -or more contributor license agreements. See the NOTICE file -distributed with this work for additional information -regarding copyright ownership. The ASF licenses this file -to you under the Apache License, Version 2.0 (the -"License"); you may not use this file except in compliance -with the License. You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, -software distributed under the License is distributed on an -"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -KIND, either express or implied. See the License for the -specific language governing permissions and limitations -under the License. -*/ -package org.apache.plc4x.java.exceptions; - -import org.apache.plc4x.java.api.exceptions.PlcConnectionException; - -/** - * Exception that should be thrown when the connection to a PLC cannot be created - */ -public class NotConnectedException extends PlcConnectionException { - public NotConnectedException(String msg) { - super(msg); - } - - public NotConnectedException(Throwable throwable) { - super(throwable); - } -} diff --git a/plc4j/core/src/test/java/org/apache/plc4x/java/connectionpool/PlcConnectionManagerTest.java b/plc4j/core/src/test/java/org/apache/plc4x/java/connectionpool/PlcConnectionManagerTest.java deleted file mode 100644 index b5f369d..0000000 --- a/plc4j/core/src/test/java/org/apache/plc4x/java/connectionpool/PlcConnectionManagerTest.java +++ /dev/null @@ -1,99 +0,0 @@ -/* -Licensed to the Apache Software Foundation (ASF) under one -or more contributor license agreements. See the NOTICE file -distributed with this work for additional information -regarding copyright ownership. The ASF licenses this file -to you under the Apache License, Version 2.0 (the -"License"); you may not use this file except in compliance -with the License. You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, -software distributed under the License is distributed on an -"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -KIND, either express or implied. See the License for the -specific language governing permissions and limitations -under the License. -*/ -package org.apache.plc4x.java.connectionpool; - -import org.apache.plc4x.java.api.connection.PlcConnection; -import org.apache.plc4x.java.api.exceptions.PlcConnectionException; -import org.apache.plc4x.java.exceptions.NoConnectionAvailableException; -import org.apache.plc4x.java.exceptions.NotConnectedException; -import org.apache.plc4x.test.FastTests; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -import org.junit.experimental.categories.Category; -import org.mockito.Mockito; - -import java.util.HashMap; -import java.util.Map; - - -public class PlcConnectionManagerTest { - - private PlcConnection plcConnection = Mockito.mock(PlcConnection.class); - private PlcConnectionManager plcConnectionManager; - private static final String TEST_STRING = "s7:127.0.0.100"; - - - @Before - public void setUp() { - Map<String, PlcConnection> plcConnectionProxyMap = new HashMap<>(); - Map<String, Boolean> booleanMap = new HashMap<>(); - plcConnectionProxyMap.put(TEST_STRING, plcConnection); - booleanMap.put(TEST_STRING, false); - plcConnectionManager = new PlcConnectionManager(plcConnectionProxyMap, booleanMap); - } - - @After - public void shutDown() { - plcConnectionManager.close(); - } - - @Test - @Category(FastTests.class) - public void getConnection() throws Exception { - plcConnectionManager.getConnection(TEST_STRING); - Mockito.verify(plcConnection, Mockito.times(1)).isConnected(); - } - - @Test(expected = NoConnectionAvailableException.class) - @Category(FastTests.class) - public void returnConnectionThrows() throws Exception { - Mockito.when(plcConnection.isConnected()).thenReturn(true); - plcConnectionManager.getConnection(TEST_STRING); - //second attempt should throw exception because connection was not returned yet - plcConnectionManager.getConnection(TEST_STRING); - } - - - @Test(expected = NotConnectedException.class) - @Category(FastTests.class) - public void returnConnectionCannotConnect() throws Exception { - Mockito.when(plcConnection.isConnected()).thenReturn(false); - PlcConnection plcConnectionProxy = plcConnectionManager.getConnection(TEST_STRING); // should - plcConnectionProxy.close(); - Mockito.doThrow(new PlcConnectionException("")).when(plcConnection).connect(); - plcConnectionManager.getConnection(TEST_STRING); - - } - - @Test - @Category(FastTests.class) - public void returnConnection() throws Exception { - Mockito.when(plcConnection.isConnected()).thenReturn(true); - plcConnectionManager.getConnection(TEST_STRING); - plcConnectionManager.returnConnection(TEST_STRING); - plcConnectionManager.getConnection(TEST_STRING); - } - - @Test - @Category(FastTests.class) - public void closeConnections() { - plcConnectionManager.close(); - } -} \ No newline at end of file diff --git a/plc4j/core/src/test/java/org/apache/plc4x/java/connectionpool/PlcConnectionProxyTest.java b/plc4j/core/src/test/java/org/apache/plc4x/java/connectionpool/PlcConnectionProxyTest.java deleted file mode 100644 index b69668d..0000000 --- a/plc4j/core/src/test/java/org/apache/plc4x/java/connectionpool/PlcConnectionProxyTest.java +++ /dev/null @@ -1,74 +0,0 @@ -/* -Licensed to the Apache Software Foundation (ASF) under one -or more contributor license agreements. See the NOTICE file -distributed with this work for additional information -regarding copyright ownership. The ASF licenses this file -to you under the Apache License, Version 2.0 (the -"License"); you may not use this file except in compliance -with the License. You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, -software distributed under the License is distributed on an -"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -KIND, either express or implied. See the License for the -specific language governing permissions and limitations -under the License. -*/ -package org.apache.plc4x.java.connectionpool; - -import org.apache.plc4x.java.api.connection.PlcConnection; -import org.junit.After; -import org.junit.Test; -import org.mockito.Mockito; - - -public class PlcConnectionProxyTest { - - private final String TEST_STRING = "s7:127.0.0.100"; - private PlcConnectionManager connectionManager = Mockito.mock(PlcConnectionManager.class); - private PlcConnection plcConnection = Mockito.mock(PlcConnection.class); - private PlcConnectionProxy plcConnectionProxy = new PlcConnectionProxy(connectionManager, TEST_STRING, plcConnection); - - @After - public void shutDown() throws Exception { - plcConnection.close(); - } - - @Test - public void connect() throws Exception { - plcConnectionProxy.connect(); - Mockito.verify(plcConnection, Mockito.times(0)).connect(); - } - - @Test - public void isConnected() { - plcConnectionProxy.isConnected(); - Mockito.verify(plcConnection, Mockito.times(1)).isConnected(); - } - - @Test - public void close() { - plcConnectionProxy.close(); - Mockito.verify(connectionManager, Mockito.times(1)).returnConnection(TEST_STRING); - } - - @Test - public void getReader() { - plcConnectionProxy.getReader(); - Mockito.verify(plcConnection, Mockito.times(1)).getReader(); - } - - @Test - public void getWriter() { - plcConnectionProxy.getWriter(); - Mockito.verify(plcConnection, Mockito.times(1)).getWriter(); - } - - @Test - public void getSubscriber() { - plcConnectionProxy.getSubscriber(); - Mockito.verify(plcConnection, Mockito.times(1)).getSubscriber(); - } -} \ No newline at end of file