| /*=============================================================================# |
| # Copyright (c) 2009, 2017 Stephan Wahlbrink and others. |
| # |
| # This program and the accompanying materials are made available under the |
| # terms of the Eclipse Public License 2.0 which is available at |
| # https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0 |
| # which is available at https://www.apache.org/licenses/LICENSE-2.0. |
| # |
| # SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 |
| # |
| # Contributors: |
| # Stephan Wahlbrink <sw@wahlbrink.eu> - initial API and implementation |
| #=============================================================================*/ |
| |
| package org.eclipse.statet.internal.rj.servi; |
| |
| import java.rmi.RemoteException; |
| import java.util.NoSuchElementException; |
| |
| import org.eclipse.core.runtime.IStatus; |
| import org.eclipse.core.runtime.Status; |
| |
| import org.eclipse.statet.jcommons.lang.Disposable; |
| |
| import org.eclipse.statet.ecommons.rmi.core.RMIRegistry; |
| import org.eclipse.statet.ecommons.runtime.core.ECommonsRuntime; |
| |
| import org.eclipse.statet.rj.RjException; |
| import org.eclipse.statet.rj.RjInitFailedException; |
| import org.eclipse.statet.rj.servi.RServi; |
| import org.eclipse.statet.rj.servi.RServiUtil; |
| import org.eclipse.statet.rj.servi.node.RServiNodeFactory; |
| import org.eclipse.statet.rj.servi.node.RServiNodeManager; |
| |
| |
| public class LocalNodeManager implements RServiNodeManager, Disposable { |
| |
| |
| private class ThisNodeHandler extends NodeHandler implements RServiImpl.PoolRef { |
| |
| @Override |
| public void returnObject(final long accessId) throws RjException, RemoteException { |
| returnRServi(accessId); |
| } |
| |
| } |
| |
| |
| private final String id; |
| |
| private final RMIRegistry registry; |
| |
| private final LocalNodeFactory factory; |
| |
| private ThisNodeHandler handler; |
| |
| private boolean inUse; |
| private long accessId; |
| |
| |
| public LocalNodeManager(final String id, final RMIRegistry registry, final LocalNodeFactory factory) { |
| if (id == null || registry == null) { |
| throw new NullPointerException(); |
| } |
| this.id= id; |
| this.registry= registry; |
| this.factory= factory; |
| |
| Utils.preLoad(); |
| } |
| |
| |
| @Override |
| public String getId() { |
| return this.id; |
| } |
| |
| @Override |
| public RServiNodeFactory getFactory() { |
| return this.factory; |
| } |
| |
| @Override |
| public void dispose() { |
| stop(); |
| } |
| |
| @Override |
| public synchronized void start() throws RjException { |
| final ThisNodeHandler poolObj= new ThisNodeHandler(); |
| try { |
| this.factory.createNode(poolObj); |
| this.handler= poolObj; |
| ECommonsRuntime.getEnv().addStoppingListener(this); |
| } |
| catch (final Throwable e) { |
| ECommonsRuntime.getEnv().log(new Status(IStatus.ERROR, RServiUtil.RJ_SERVI_ID, |
| Messages.StartNode_error_message, e )); |
| throw new RjInitFailedException(Messages.StartLocal_pub_error_message, |
| (e instanceof RjException) ? e : null); |
| } |
| } |
| |
| @Override |
| public synchronized void stop() { |
| if (this.handler == null) { |
| return; |
| } |
| ECommonsRuntime.getEnv().removeStoppingListener(this); |
| if (this.inUse) { |
| returnRServi(this.accessId); |
| if (this.handler == null) { |
| return; |
| } |
| } |
| this.factory.stopNode(this.handler); |
| this.handler= null; |
| } |
| |
| @Override |
| public synchronized RServi getRServi(final String name) throws NoSuchElementException, RjException { |
| if (this.handler == null) { |
| start(); |
| } |
| if (this.inUse) { |
| throw new NoSuchElementException(Messages.GetRServi_NoInstance_pub_Single_message); |
| } |
| try { |
| this.handler.bindClient(name, "local"); |
| } |
| catch (final Throwable e) { |
| ECommonsRuntime.getEnv().log(new Status(IStatus.ERROR, RServiUtil.RJ_SERVI_ID, |
| Messages.BindClient_error_message, e )); |
| throw new RjException(Messages.GetRServi_pub_error_message); |
| } |
| this.inUse= true; |
| return new RServiImpl(this.accessId, this.handler, this.handler.getClientHandler()); |
| } |
| |
| private synchronized void returnRServi(final long accessId) { |
| if (this.handler == null) { |
| return; |
| } |
| if (this.accessId != accessId) { |
| throw new IllegalStateException("Access id no longer valid."); |
| } |
| this.inUse= false; |
| this.accessId++; |
| try { |
| this.handler.unbindClient(); |
| } |
| catch (final Throwable e) { |
| ECommonsRuntime.getEnv().log(new Status(IStatus.ERROR, RServiUtil.RJ_SERVI_ID, |
| Messages.UnbindClient_error_message, e )); |
| stop(); |
| } |
| } |
| |
| } |