package org.eclipse.osgi.container;
import java.util.Collections;
import java.util.EnumSet;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;
import org.eclipse.osgi.container.ModuleContainer.ContainerStartLevel;
import org.eclipse.osgi.container.ModuleContainerAdaptor.ContainerEvent;
import org.eclipse.osgi.container.ModuleContainerAdaptor.ModuleEvent;
import org.eclipse.osgi.internal.messages.Msg;
import org.eclipse.osgi.report.resolution.ResolutionReport;
import org.osgi.framework.AdminPermission;
import org.osgi.framework.BundleException;
import org.osgi.framework.Constants;
import org.osgi.framework.launch.Framework;
import org.osgi.service.resolver.ResolutionException;
public abstract class SystemModule extends Module {
private volatile AtomicReference<ContainerEvent> forStop = new AtomicReference<>();
public SystemModule(ModuleContainer container) {
super(new Long(0), Constants.SYSTEM_BUNDLE_LOCATION, container, EnumSet.of(Settings.AUTO_START, Settings.USE_ACTIVATION_POLICY), Integer.valueOf(0));
}
public final void init() throws BundleException {
getRevisions().getContainer().checkAdminPermission(getBundle(), AdminPermission.EXECUTE);
boolean lockedStarted = false;
inStart.incrementAndGet();
try {
lockStateChange(ModuleEvent.STARTED);
lockedStarted = true;
getContainer().getAdaptor().initBegin();
checkValid();
if (ACTIVE_SET.contains(getState()))
return;
getRevisions().getContainer().open();
if (getState().equals(State.INSTALLED)) {
unlockStateChange(ModuleEvent.STARTED);
lockedStarted = false;
ResolutionReport report;
try {
report = getRevisions().getContainer().resolve(Collections.singletonList((Module) this), true);
} finally {
lockStateChange(ModuleEvent.STARTED);
lockedStarted = true;
}
checkValid();
ResolutionException e = report.getResolutionException();
if (e != null) {
if (e.getCause() instanceof BundleException) {
throw (BundleException) e.getCause();
}
}
if (ACTIVE_SET.contains(getState()))
return;
if (getState().equals(State.INSTALLED)) {
String reportMessage = report.getResolutionReportMessage(getCurrentRevision());
throw new BundleException(Msg.Module_ResolveError + reportMessage, BundleException.RESOLVE_ERROR);
}
}
setState(State.STARTING);
AtomicReference<ContainerEvent> existingForStop = forStop;
if (existingForStop.get() != null) {
forStop = new AtomicReference<>();
}
publishEvent(ModuleEvent.STARTING);
try {
initWorker();
} catch (Throwable t) {
setState(State.STOPPING);
publishEvent(ModuleEvent.STOPPING);
setState(State.RESOLVED);
publishEvent(ModuleEvent.STOPPED);
getRevisions().getContainer().close();
if (t instanceof BundleException) {
throw (BundleException) t;
}
throw new BundleException("Error initializing container.", BundleException.ACTIVATOR_ERROR, t);
}
} finally {
getContainer().getAdaptor().initEnd();
if (lockedStarted) {
unlockStateChange(ModuleEvent.STARTED);
}
inStart.decrementAndGet();
}
}
public ContainerEvent waitForStop(long timeout) throws InterruptedException {
final boolean waitForever = timeout == 0;
final long start = System.currentTimeMillis();
long timeLeft = timeout;
AtomicReference<ContainerEvent> stopEvent = null;
State currentState = null;
boolean stateLocked = false;
try {
if (timeout == 0) {
stateChangeLock.lockInterruptibly();
stateLocked = true;
} else {
stateLocked = stateChangeLock.tryLock(timeLeft, TimeUnit.MILLISECONDS);
}
if (stateLocked) {
stopEvent = forStop;
currentState = getState();
}
} finally {
if (stateLocked) {
stateChangeLock.unlock();
}
}
if (stopEvent == null || currentState == null) {
return ContainerEvent.STOPPED_TIMEOUT;
}
if (!ACTIVE_SET.contains(currentState)) {
ContainerEvent result = stopEvent.get();
if (result != null) {
return result;
}
return ContainerEvent.STOPPED;
}
synchronized (stopEvent) {
do {
ContainerEvent result = stopEvent.get();
if (result != null) {
return result;
}
timeLeft = waitForever ? 0 : start + timeout - System.currentTimeMillis();
if (waitForever || timeLeft > 0) {
stopEvent.wait(timeLeft);
} else {
return ContainerEvent.STOPPED_TIMEOUT;
}
} while (true);
}
}
protected void initWorker() throws BundleException {
}
@Override
public void start(StartOptions... options) throws BundleException {
init();
super.start(StartOptions.TRANSIENT, StartOptions.USE_ACTIVATION_POLICY);
getRevisions().getContainer().adaptor.publishContainerEvent(ContainerEvent.STARTED, this, null);
}
@Override
public void stop(StopOptions... options) throws BundleException {
ContainerEvent containerEvent = ContainerEvent.STOPPED_TIMEOUT;
try {
if (stateChangeLock.tryLock(10, TimeUnit.SECONDS)) {
try {
try {
super.stop(StopOptions.TRANSIENT);
} catch (BundleException e) {
getRevisions().getContainer().adaptor.publishContainerEvent(ContainerEvent.ERROR, this, e);
}
if (holdsTransitionEventLock(ModuleEvent.UPDATED)) {
containerEvent = ContainerEvent.STOPPED_UPDATE;
} else if (holdsTransitionEventLock(ModuleEvent.UNRESOLVED)) {
containerEvent = ContainerEvent.STOPPED_REFRESH;
} else {
containerEvent = ContainerEvent.STOPPED;
}
getRevisions().getContainer().adaptor.publishContainerEvent(containerEvent, this, null);
getRevisions().getContainer().close();
} finally {
AtomicReference<ContainerEvent> eventReference = forStop;
eventReference.compareAndSet(null, containerEvent);
stateChangeLock.unlock();
synchronized (eventReference) {
eventReference.notifyAll();
}
}
} else {
throw new BundleException(Msg.SystemModule_LockError);
}
} catch (InterruptedException e) {
getRevisions().getContainer().adaptor.publishContainerEvent(ContainerEvent.ERROR, this, e);
throw new BundleException(Msg.Module_LockError + toString(), BundleException.STATECHANGE_ERROR, e);
}
}
public void update() throws BundleException {
getContainer().checkAdminPermission(getBundle(), AdminPermission.LIFECYCLE);
State previousState;
lockStateChange(ModuleEvent.UPDATED);
try {
previousState = getState();
stop();
} finally {
unlockStateChange(ModuleEvent.UPDATED);
}
switch (previousState) {
case STARTING :
init();
break;
case ACTIVE :
start();
default :
break;
}
}
@Override
protected void startWorker() throws BundleException {
super.startWorker();
((ContainerStartLevel) getRevisions().getContainer().getFrameworkStartLevel()).doContainerStartLevel(this, ContainerStartLevel.USE_BEGINNING_START_LEVEL);
}
@Override
protected void stopWorker() throws BundleException {
super.stopWorker();
((ContainerStartLevel) getRevisions().getContainer().getFrameworkStartLevel()).doContainerStartLevel(this, 0);
}
}