From a92115a4502597730f06e0ed1e7c326019f5b27f Mon Sep 17 00:00:00 2001 From: Olivier Capillon Date: Fri, 11 Jun 2010 16:49:11 +0000 Subject: [PATCH] Simplify DetachedExecutionServerImpl, make DetachedAdminCommand optional, correct Update/refresh of bundles. git-svn-id: https://svn.argeo.org/slc/trunk@3603 4cfe0d0a-d680-48aa-b62c-e0a02a3f76cc --- .../detached/DetachedExecutionServerImpl.java | 258 +++++++----------- .../argeo/slc/detached/DetachedSession.java | 25 +- .../detached/DetachedXmlConverterCompat.java | 4 +- .../detached/admin/MinimalBundlesManager.java | 163 +++++------ .../argeo/slc/detached/admin/OpenSession.java | 50 ++-- .../slc/lib/detached/DetachedLauncher.java | 4 +- .../slc/lib/detached/DetachedTestData.java | 4 + .../lib/detached/DetachedTestDefinition.java | 34 ++- 8 files changed, 235 insertions(+), 307 deletions(-) diff --git a/runtime/org.argeo.slc.detached/src/main/java/org/argeo/slc/detached/DetachedExecutionServerImpl.java b/runtime/org.argeo.slc.detached/src/main/java/org/argeo/slc/detached/DetachedExecutionServerImpl.java index 4a1ddec98..d1cc3abf9 100644 --- a/runtime/org.argeo.slc.detached/src/main/java/org/argeo/slc/detached/DetachedExecutionServerImpl.java +++ b/runtime/org.argeo.slc.detached/src/main/java/org/argeo/slc/detached/DetachedExecutionServerImpl.java @@ -1,15 +1,11 @@ package org.argeo.slc.detached; -import java.io.PrintWriter; -import java.io.StringWriter; import java.util.Collections; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; -import java.util.Vector; -import org.apache.commons.io.IOUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.argeo.slc.detached.admin.CloseSession; @@ -31,10 +27,14 @@ public class DetachedExecutionServerImpl implements DetachedExecutionServer, .getLog(DetachedExecutionServerImpl.class); private final DetachedContextImpl detachedContext; - private final List sessions; - private int skipCount = 1;// start skipCount at 1 since the first step is - // always an open session + private DetachedSession currentSession; + + /** + * Session being replayed, skipping the steps in the current session. If + * null, no session is replayed + */ + private DetachedSession replayedSession = null; private BundleContext bundleContext; private ApplicationContext applicationContext; @@ -46,11 +46,13 @@ public class DetachedExecutionServerImpl implements DetachedExecutionServer, public DetachedExecutionServerImpl() { detachedContext = new DetachedContextImpl(); - sessions = new Vector(); + currentSession = new DetachedSession(); + currentSession.setUuid(Long.toString(System.currentTimeMillis())); } public synchronized DetachedAnswer executeRequest(DetachedRequest request) { - log.info("Received " + request); + if(log.isDebugEnabled()) + log.debug("Received " + request); DetachedAnswer answer = null; try { @@ -77,34 +79,13 @@ public class DetachedExecutionServerImpl implements DetachedExecutionServer, } catch (Exception e) { answer = new DetachedAnswer(request); answer.setStatus(DetachedAnswer.ERROR); - StringWriter writer = new StringWriter(); - e.printStackTrace(new PrintWriter(writer)); - answer.setLog(writer.toString()); - IOUtils.closeQuietly(writer); - } - - // Case where current session is unexpectedly null - if (getCurrentSession() == null) { - log - .error("CURRENT SESSION IS NULL." - + " Detached status is inconsistent dumping sessions history:"); - log.error(dumpSessionsHistory(request, answer)); - if (answer != null) { - answer.setStatus(DetachedAnswer.ERROR); - answer - .addToLog("CURRENT SESSION IS NULL." - + " Detached status is inconsistent, see detached log for more details."); - return answer; - } else { - throw new DetachedException( - "Answer is null. Cannot return it. See log for more details."); - } - + log.error("Error executing request " + request, e); } - getCurrentSession().getRequests().add(request); - getCurrentSession().getAnswers().add(answer); - log.info("Sent " + answer); + currentSession.getRequests().add(request); + currentSession.getAnswers().add(answer); + if(log.isDebugEnabled()) + log.debug("Sent " + answer); return answer; } @@ -195,26 +176,6 @@ public class DetachedExecutionServerImpl implements DetachedExecutionServer, } } - // ServiceReference[] refs = bundleContext.getAllServiceReferences( - // ApplicationContext.class.getName(), null); - // Object obj = null; - // for (int i = 0; i < refs.length; i++) { - // ApplicationContext appContext = (ApplicationContext) - // bundleContext - // .getService(refs[i]); - // try { - // obj = appContext.getBean(request.getRef()); - // } catch (Exception e) { - // // silent - // if (log.isTraceEnabled()) - // log.trace("Could not find ref " + request.getRef(), e); - // } - // if (obj != null) { - // break; - // } - // } - // return obj; - throw new Exception( "Cannot find any published application context containing bean " + request.getRef()); @@ -223,49 +184,61 @@ public class DetachedExecutionServerImpl implements DetachedExecutionServer, protected synchronized DetachedAnswer processStep(DetachedStep obj, DetachedRequest request) { DetachedAnswer answer; - if (getCurrentSession() == null) - throw new DetachedException("No open session."); - + StringBuffer skippedLog = new StringBuffer(); boolean execute = true; - if (getPreviousSession() != null && !getPreviousSession().isClosed()) { - if (getCurrentSession().getDoItAgainPolicy().equals( - DetachedSession.SKIP_UNTIL_ERROR)) { - // Skip execution of already successful steps - if (getPreviousSession().getAnswers().size() > skipCount) { - DetachedAnswer previousAnswer = (DetachedAnswer) getPreviousSession() - .getAnswers().get(skipCount); - DetachedRequest previousRequest = (DetachedRequest) getPreviousSession() - .getRequests().get(skipCount); - // Check paths - if (!previousRequest.getPath().equals(request.getPath())) { - String msg = "New request is not consistent with previous path. previousPath=" - + previousRequest.getPath() - + ", newPath=" - + request.getPath() + "\n"; - skippedLog.append(msg); - log.warn(msg); - } - - if (previousAnswer.getStatus() != DetachedAnswer.ERROR) { - execute = false; - String msg = "Skipped path " + request.getPath() - + " (skipCount=" + skipCount + ")"; - skippedLog.append(msg); - log.info(msg); - skipCount++; - } else { - log - .info("Path " - + request.getPath() - + " was previously in error, executing it again." - + " (skipCount=" + skipCount - + "). Reset skip count to 1"); - skipCount = 1; - } + + if (replayedSession != null) { + // Skip execution of already successful steps + int stepIndex = currentSession.getExecutedStepCount(); + + if (stepIndex < replayedSession.getExecutedStepCount()) { + DetachedAnswer previousAnswer = (DetachedAnswer) replayedSession + .getAnswers().get(stepIndex); + DetachedRequest previousRequest = (DetachedRequest) replayedSession + .getRequests().get(stepIndex); + + // check step names + if (!previousRequest.getRef().equals(request.getRef())) { + String msg = "New request is not consistent with previous ref. previousRef=" + + previousRequest.getRef() + + ", newRef=" + + request.getRef() + "\n"; + skippedLog.append(msg); + log.warn(msg); + } + + // Check paths + if (!previousRequest.getPath().equals(request.getPath())) { + String msg = "New request is not consistent with previous path. previousPath=" + + previousRequest.getPath() + + ", newPath=" + + request.getPath() + "\n"; + skippedLog.append(msg); + log.warn(msg); + } + + if (previousAnswer.getStatus() != DetachedAnswer.ERROR) { + // if no error occurred in the replayedSession, + // skip the step + execute = false; + String msg = "Skipped Step " + request.getRef() + + " (stepIndex=" + stepIndex + ")"; + skippedLog.append(msg); + log.info(msg); + } else { - // went further as skip count, doing nothing. + // if an error occurred, execute the step and leave + // skipUntillError mode (even if replayedSession + // has more steps) + log.info("### End of SkipUntilError Mode ###"); + log.info("Step " + request.getRef() + + " was previously in error, executing it again." + + " (stepIndex=" + stepIndex + ")."); + replayedSession = null; } + } else { + // went further as skip count, doing nothing. } } @@ -285,27 +258,27 @@ public class DetachedExecutionServerImpl implements DetachedExecutionServer, DetachedAdminCommand obj, DetachedRequest request) { DetachedAnswer answer; if (obj instanceof OpenSession) { - if (getCurrentSession() != null) { - // TODO: better understand why there is sometimes two open - // sessions sent. - log.warn("There is already an open session #" - + getCurrentSession().getUuid() + ". Closing it..."); - DetachedAnswer answerT = new DetachedAnswer( - request, - "Session #" - + getCurrentSession().getUuid() - + " forcibly closed. THIS ANSWER WAS NOT SENT BACK."); - answerT.setStatus(DetachedAnswer.CLOSED_SESSION); - getCurrentSession().getAnswers().add(answerT); + DetachedSession newSession = ((OpenSession) obj).execute(request, + bundleContext); + + log.debug("Creating new DetachedSession : " + newSession); + + if ((currentSession != null) && currentSession.lastActionIsError() + && DetachedSession.SKIP_UNTIL_ERROR.equals(newSession.getDoItAgainPolicy())) { + // switch to replay mode + log.info("### Start SkipUntilError Mode ###"); + replayedSession = currentSession; } - sessions.add(((OpenSession) obj).execute(request, bundleContext)); + + currentSession = newSession; + answer = new DetachedAnswer(request, "Session #" - + getCurrentSession().getUuid() + " open."); + + currentSession.getUuid() + " open."); } else if (obj instanceof CloseSession) { - if (getCurrentSession() == null) + if (currentSession == null) throw new DetachedException("There is no open session to close"); answer = new DetachedAnswer(request, "Session #" - + getCurrentSession().getUuid() + " closed."); + + currentSession.getUuid() + " closed."); answer.setStatus(DetachedAnswer.CLOSED_SESSION); } else { answer = null; @@ -313,56 +286,28 @@ public class DetachedExecutionServerImpl implements DetachedExecutionServer, return answer; } - /** - * Returns the current session based on the list of previous sessions. - * - * @return the current session or null if there is no session yet defined or - * if the last registered session is null or in error. - */ - protected synchronized final DetachedSession getCurrentSession() { - if (sessions.size() == 0) { - return null; - } else { - DetachedSession session = (DetachedSession) sessions.get(sessions - .size() - 1); - List answers = session.getAnswers(); - if (answers.size() > 0) { - DetachedAnswer lastAnswer = (DetachedAnswer) answers - .get(answers.size() - 1); - if (lastAnswer.getStatus() == DetachedAnswer.ERROR - || lastAnswer.getStatus() == DetachedAnswer.CLOSED_SESSION) - return null; - } - return session; - } - } - protected synchronized String dumpSessionsHistory( DetachedRequest requestCurrent, DetachedAnswer answerCurrent) { StringBuffer buf = new StringBuffer( "##\n## SESSIONS HISTORY DUMP\n##\n"); buf.append("# CURRENT\n"); - buf.append("Current session: ").append(getCurrentSession()) + buf.append("Current session: ").append(currentSession) .append('\n'); buf.append("Current request: ").append(requestCurrent).append('\n'); buf.append("Current answer: ").append(answerCurrent).append('\n'); - buf.append("Skip count: ").append(skipCount).append('\n'); - - buf.append("# SESSIONS\n"); - for (int i = 0; i < sessions.size(); i++) { - DetachedSession session = (DetachedSession) sessions.get(i); - buf.append(i).append(". ").append(session).append('\n'); - List requests = session.getRequests(); - List answers = session.getAnswers(); - for (int j = 0; j < requests.size(); j++) { - DetachedRequest request = (DetachedRequest) requests.get(j); - buf.append('\t').append(j).append(". ").append(request).append( + + buf.append("# CURRENT SESSION\n"); + + List requests = currentSession.getRequests(); + List answers = currentSession.getAnswers(); + for (int j = 0; j < requests.size(); j++) { + DetachedRequest request = (DetachedRequest) requests.get(j); + buf.append('\t').append(j).append(". ").append(request) + .append('\n'); + if (answers.size() > j) { + DetachedAnswer answer = (DetachedAnswer) answers.get(j); + buf.append('\t').append(j).append(". ").append(answer).append( '\n'); - if (answers.size() > j) { - DetachedAnswer answer = (DetachedAnswer) answers.get(j); - buf.append('\t').append(j).append(". ").append(answer) - .append('\n'); - } } } @@ -373,19 +318,12 @@ public class DetachedExecutionServerImpl implements DetachedExecutionServer, return buf.toString(); } - protected synchronized final DetachedSession getPreviousSession() { - if (sessions.size() < 2) - return null; - else - return (DetachedSession) sessions.get(sessions.size() - 2); - } - public void setBundleContext(BundleContext bundleContext) { this.bundleContext = bundleContext; } public void afterPropertiesSet() throws Exception { - log.info("Detached execution server initialized."); + log.debug("Detached execution server initialized."); } public synchronized void destroy() throws Exception { @@ -398,7 +336,7 @@ public class DetachedExecutionServerImpl implements DetachedExecutionServer, } appContextServiceTrackers.clear(); - log.info("Detached execution server closed."); + log.debug("Detached execution server closed."); } public void setApplicationContext(ApplicationContext applicationContext) { diff --git a/runtime/org.argeo.slc.detached/src/main/java/org/argeo/slc/detached/DetachedSession.java b/runtime/org.argeo.slc.detached/src/main/java/org/argeo/slc/detached/DetachedSession.java index 5c7884fe5..a3931eb47 100644 --- a/runtime/org.argeo.slc.detached/src/main/java/org/argeo/slc/detached/DetachedSession.java +++ b/runtime/org.argeo.slc.detached/src/main/java/org/argeo/slc/detached/DetachedSession.java @@ -1,5 +1,6 @@ package org.argeo.slc.detached; +import java.util.ArrayList; import java.util.List; import java.util.Vector; @@ -12,7 +13,8 @@ public class DetachedSession { private String uuid = null; private List requests = new Vector(); private List answers = new Vector(); - private String doItAgainPolicy = SKIP_UNTIL_ERROR; + private String doItAgainPolicy = REPLAY; + private List refreshedBundleNames = new ArrayList(); public boolean isClosed() { if (answers.size() > 0) { @@ -23,6 +25,23 @@ public class DetachedSession { return false; } } + + public boolean lastActionIsError() { + if (answers.size() > 0) { + DetachedAnswer answer = (DetachedAnswer) answers + .get(answers.size() - 1); + return answer.getStatus() == DetachedAnswer.ERROR; + } else { + return false; + } + } + + public int getExecutedStepCount() { + if(requests.size() != answers.size()) { + throw new DetachedException("requests.size() != answers.size() in DetachedSession"); + } + return answers.size(); + } public String getDoItAgainPolicy() { return doItAgainPolicy; @@ -48,6 +67,10 @@ public class DetachedSession { return answers; } + public List getRefreshedBundleNames() { + return refreshedBundleNames; + } + public String toString() { StringBuffer buf = new StringBuffer(getClass().getName()); buf.append("#").append(uuid); diff --git a/runtime/org.argeo.slc.detached/src/main/java/org/argeo/slc/detached/DetachedXmlConverterCompat.java b/runtime/org.argeo.slc.detached/src/main/java/org/argeo/slc/detached/DetachedXmlConverterCompat.java index a306fa8f8..b2bdf8e4f 100644 --- a/runtime/org.argeo.slc.detached/src/main/java/org/argeo/slc/detached/DetachedXmlConverterCompat.java +++ b/runtime/org.argeo.slc.detached/src/main/java/org/argeo/slc/detached/DetachedXmlConverterCompat.java @@ -61,13 +61,13 @@ public class DetachedXmlConverterCompat implements DetachedXmlConverter { throw new DetachedException("Could not copy xml source", e); } - if (log.isDebugEnabled()) { + if (log.isTraceEnabled()) { try { DOMSource domSource = new DOMSource(result.getNode()); StringWriter stringWriter = new StringWriter(); StreamResult streamResult = new StreamResult(stringWriter); copy.transform(domSource, streamResult); - log.debug("Unmarshall communication:\n" + log.trace("Unmarshall communication:\n" + stringWriter.toString()); IOUtils.closeQuietly(stringWriter); } catch (TransformerException e) { diff --git a/runtime/org.argeo.slc.detached/src/main/java/org/argeo/slc/detached/admin/MinimalBundlesManager.java b/runtime/org.argeo.slc.detached/src/main/java/org/argeo/slc/detached/admin/MinimalBundlesManager.java index 602fe1543..5cac6c7e5 100644 --- a/runtime/org.argeo.slc.detached/src/main/java/org/argeo/slc/detached/admin/MinimalBundlesManager.java +++ b/runtime/org.argeo.slc.detached/src/main/java/org/argeo/slc/detached/admin/MinimalBundlesManager.java @@ -1,5 +1,7 @@ package org.argeo.slc.detached.admin; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; import org.osgi.framework.Bundle; import org.osgi.framework.BundleContext; import org.osgi.framework.BundleException; @@ -14,13 +16,14 @@ import org.osgi.service.packageadmin.PackageAdmin; * class in org.argeo.slc.support.osgi. */ class MinimalBundlesManager implements FrameworkListener { + private final static Log log = LogFactory + .getLog(MinimalBundlesManager.class); + private final BundleContext bundleContext; private long defaultTimeout = 10000l; private final Object refreshedPackageSem = new Object(); - private boolean debug = true; - public MinimalBundlesManager(BundleContext bundleContext) { this.bundleContext = bundleContext; bundleContext.addFrameworkListener(this); @@ -31,35 +34,64 @@ class MinimalBundlesManager implements FrameworkListener { } /** - * Stop the module, update it, refresh it and restart it. All synchronously. + * @see #upgradeSynchronous(Bundle[]) */ public void upgradeSynchronous(Bundle bundle) { + upgradeSynchronous(new Bundle[] { bundle }); + } + + /** + * Stop the active bundles, update them, refresh them and restart the + * initially active bundles. All synchronously. + */ + public void upgradeSynchronous(Bundle[] bundles) { try { - stopSynchronous(bundle); - updateSynchronous(bundle); - // Refresh in case there are fragments - refreshSynchronous(bundle); - startSynchronous(bundle); - - String filter = "(Bundle-SymbolicName=" + bundle.getSymbolicName() - + ")"; - // Wait for application context to be ready - // TODO: use service tracker - getServiceRefSynchronous( - "org.springframework.context.ApplicationContext", filter); - - if (debug) - debug("Bundle " + bundle.getSymbolicName() - + " ready to be used at latest version."); + // State (ACTIVE or other) before upgrading + int[] initialStates = new int[bundles.length]; + + // store initial state and stop active bundles + for (int i = 0; i < bundles.length; ++i) { + initialStates[i] = bundles[i].getState(); + if (initialStates[i] == Bundle.ACTIVE) { + stopSynchronous(bundles[i]); + } + } + + // update the bundles + for (int i = 0; i < bundles.length; ++i) { + updateSynchronous(bundles[i]); + } + + // refresh the bundles + refreshSynchronous(bundles); + + // restart the bundles that were ACTIVE before upgrading + for (int i = 0; i < bundles.length; ++i) { + if (initialStates[i] == Bundle.ACTIVE) { + startSynchronous(bundles[i]); + + String filter = "(Bundle-SymbolicName=" + + bundles[i].getSymbolicName() + ")"; + // Wait for application context to be ready + // TODO: use service tracker + try { + getServiceRefSynchronous( + "org.springframework.context.ApplicationContext", + filter); + } + // in case of exception, catch and go on + catch (Exception e) { + log.error("getServiceRefSynchronous failed", e); + } + } + } } catch (Exception e) { - throw new RuntimeException("Cannot update bundle " - + bundle.getSymbolicName(), e); + throw new RuntimeException("Cannot update bundles", e); } } /** Updates bundle synchronously. */ protected void updateSynchronous(Bundle bundle) throws BundleException { - // int originalState = bundle.getState(); bundle.update(); boolean waiting = true; @@ -77,8 +109,8 @@ class MinimalBundlesManager implements FrameworkListener { + " timed out. Bundle state = " + bundle.getState()); } while (waiting); - if (debug) - debug("Bundle " + bundle.getSymbolicName() + " updated."); + if (log.isDebugEnabled()) + log.debug("Bundle " + bundle.getSymbolicName() + " updated."); } /** Starts bundle synchronously. Does nothing if already started. */ @@ -102,8 +134,8 @@ class MinimalBundlesManager implements FrameworkListener { + " timed out. Bundle state = " + bundle.getState()); } while (waiting); - if (debug) - debug("Bundle " + bundle.getSymbolicName() + " started."); + if (log.isDebugEnabled()) + log.debug("Bundle " + bundle.getSymbolicName() + " started."); } /** Stops bundle synchronously. Does nothing if already started. */ @@ -128,17 +160,16 @@ class MinimalBundlesManager implements FrameworkListener { + " timed out. Bundle state = " + bundle.getState()); } while (waiting); - if (debug) - debug("Bundle " + bundle.getSymbolicName() + " stopped."); + if (log.isDebugEnabled()) + log.debug("Bundle " + bundle.getSymbolicName() + " stopped."); } /** Refresh bundle synchronously. Does nothing if already started. */ - protected void refreshSynchronous(Bundle bundle) throws BundleException { + protected void refreshSynchronous(Bundle[] bundles) throws BundleException { ServiceReference packageAdminRef = bundleContext .getServiceReference(PackageAdmin.class.getName()); PackageAdmin packageAdmin = (PackageAdmin) bundleContext .getService(packageAdminRef); - Bundle[] bundles = { bundle }; packageAdmin.refreshPackages(bundles); synchronized (refreshedPackageSem) { @@ -149,8 +180,8 @@ class MinimalBundlesManager implements FrameworkListener { } } - if (debug) - debug("Bundle " + bundle.getSymbolicName() + " refreshed."); + if (log.isDebugEnabled()) + log.debug("Bundles refreshed."); } public void frameworkEvent(FrameworkEvent event) { @@ -163,8 +194,8 @@ class MinimalBundlesManager implements FrameworkListener { public ServiceReference[] getServiceRefSynchronous(String clss, String filter) throws InvalidSyntaxException { - if (debug) - debug("Filter: '" + filter + "'"); + if (log.isTraceEnabled()) + log.trace("Filter: '" + filter + "'"); ServiceReference[] sfs = null; boolean waiting = true; long begin = System.currentTimeMillis(); @@ -183,70 +214,6 @@ class MinimalBundlesManager implements FrameworkListener { return sfs; } - /* - * protected void sleep(long ms) { try { Thread.sleep(ms); } catch - * (InterruptedException e) { // silent } } - * - * public ServiceTracker newTracker(Class clss) { ServiceTracker st = new - * ServiceTracker(bundleContext, clss.getName(), null); st.open(); return - * st; } - * - * @SuppressWarnings(value = { "unchecked" }) public T - * getSingleService(Class clss, String filter) { - * Assert.isTrue(OsgiFilterUtils.isValidFilter(filter), "valid filter"); - * ServiceReference[] sfs; try { sfs = - * bundleContext.getServiceReferences(clss.getName(), filter); } catch - * (InvalidSyntaxException e) { throw new - * SlcException("Cannot retrieve service reference for " + filter, e); } - * - * if (sfs == null || sfs.length == 0) return null; else if (sfs.length > 1) - * throw new SlcException("More than one execution flow found for " + - * filter); return (T) bundleContext.getService(sfs[0]); } - * - * public T getSingleServiceStrict(Class clss, String filter) { T - * service = getSingleService(clss, filter); if (service == null) throw new - * SlcException("No execution flow found for " + filter); else return - * service; } - * - * public Bundle findRelatedBundle(OsgiBundle osgiBundle) { Bundle bundle = - * null; if (osgiBundle.getInternalBundleId() != null) { bundle = - * bundleContext.getBundle(osgiBundle.getInternalBundleId()); Assert.isTrue( - * osgiBundle.getName().equals(bundle.getSymbolicName()), - * "symbolic name consistent"); - * Assert.isTrue(osgiBundle.getVersion().equals( - * bundle.getHeaders().get(Constants.BUNDLE_VERSION)), - * "version consistent"); } else { for (Bundle b : - * bundleContext.getBundles()) { if - * (b.getSymbolicName().equals(osgiBundle.getName())) { if - * (b.getHeaders().get(Constants.BUNDLE_VERSION).equals( - * osgiBundle.getVersion())) { bundle = b; - * osgiBundle.setInternalBundleId(b.getBundleId()); } } } } return bundle; } - * - * public OsgiBundle findFromPattern(String pattern) { OsgiBundle osgiBundle - * = null; for (Bundle b : bundleContext.getBundles()) { if - * (b.getSymbolicName().contains(pattern)) { osgiBundle = new OsgiBundle(b); - * break; } } return osgiBundle; } - * - * public OsgiBundle getBundle(Long bundleId) { Bundle bundle = - * bundleContext.getBundle(bundleId); return new OsgiBundle(bundle); } - * - * public void setBundleContext(BundleContext bundleContext) { - * this.bundleContext = bundleContext; } - * - * public void afterPropertiesSet() throws Exception { - * bundleContext.addFrameworkListener(this); } - * - * public void setDefaultTimeout(Long defaultTimeout) { this.defaultTimeout - * = defaultTimeout; } - * - * BundleContext getBundleContext() { return bundleContext; } - */ - - protected void debug(Object obj) { - if (debug) - System.out.println("#OSGiMANAGER DEBUG# " + obj); - } - protected void sleep(long ms) { try { Thread.sleep(ms); diff --git a/runtime/org.argeo.slc.detached/src/main/java/org/argeo/slc/detached/admin/OpenSession.java b/runtime/org.argeo.slc.detached/src/main/java/org/argeo/slc/detached/admin/OpenSession.java index f6522e423..6c9ae5c7b 100644 --- a/runtime/org.argeo.slc.detached/src/main/java/org/argeo/slc/detached/admin/OpenSession.java +++ b/runtime/org.argeo.slc.detached/src/main/java/org/argeo/slc/detached/admin/OpenSession.java @@ -1,7 +1,7 @@ package org.argeo.slc.detached.admin; -import java.util.ArrayList; import java.util.List; +import java.util.ArrayList; import java.util.Properties; import java.util.StringTokenizer; @@ -19,7 +19,6 @@ public class OpenSession implements DetachedAdminCommand { public DetachedSession execute(DetachedRequest request, BundleContext bundleContext) { - MinimalBundlesManager bundlesManager = new MinimalBundlesManager(bundleContext); DetachedSession session = new DetachedSession(); session.setUuid(Long.toString(System.currentTimeMillis())); @@ -32,43 +31,36 @@ public class OpenSession implements DetachedAdminCommand { String refreshedBundles = props .getProperty("slc.detached.refreshedBundles"); if (refreshedBundles != null) { + List refreshedBundleNames = new ArrayList(); StringTokenizer st = new StringTokenizer(refreshedBundles, ","); while (st.hasMoreTokens()) { refreshedBundleNames.add(st.nextElement()); - } + } - Bundle[] bundles = bundleContext.getBundles(); + Bundle[] allBundles = bundleContext.getBundles(); + Bundle[] bundlesToRefresh = new Bundle[refreshedBundleNames.size()]; - List bundlesToRefresh = new ArrayList(); - for (int i = 0; i < bundles.length; i++) { - if (refreshedBundleNames.contains(bundles[i].getSymbolicName())) { - bundlesToRefresh.add(bundles[i]); - } + log.debug("Bundles to refresh for DetachedSession:"); + + for(int i = 0; i < bundlesToRefresh.length; ++i) { + bundlesToRefresh[i] = getBundleForName((String)refreshedBundleNames.get(i), allBundles); + if(log.isDebugEnabled()) + log.debug(" " + refreshedBundleNames.get(i)); } - for (int i = 0; i < bundlesToRefresh.size(); i++) { - Bundle bundle = (Bundle) bundlesToRefresh.get(i); - try { - bundlesManager.upgradeSynchronous(bundle); -// bundle.stop(); -// bundle.update(); -// bundle.start(); - log.info("Refreshed bundle " + bundle.getSymbolicName()); - -// try { -// Thread.sleep(2000); -// } catch (InterruptedException e) { -// e.printStackTrace(); -// } - - } catch (Exception e) { - throw new DetachedException("Could not refresh bundle " - + bundle.getSymbolicName(), e); - } - } + (new MinimalBundlesManager(bundleContext)).upgradeSynchronous(bundlesToRefresh); } return session; } + + private Bundle getBundleForName(String symbolicName, Bundle[] bundles) { + for (int i = 0; i < bundles.length; i++) { + if (symbolicName.equals(bundles[i].getSymbolicName())) { + return bundles[i]; + } + } + throw new DetachedException("No Bundle found for symbolic name " + symbolicName); + } } diff --git a/runtime/org.argeo.slc.lib.detached/src/main/java/org/argeo/slc/lib/detached/DetachedLauncher.java b/runtime/org.argeo.slc.lib.detached/src/main/java/org/argeo/slc/lib/detached/DetachedLauncher.java index 85fabfdb5..daf99b374 100644 --- a/runtime/org.argeo.slc.lib.detached/src/main/java/org/argeo/slc/lib/detached/DetachedLauncher.java +++ b/runtime/org.argeo.slc.lib.detached/src/main/java/org/argeo/slc/lib/detached/DetachedLauncher.java @@ -92,7 +92,7 @@ public class DetachedLauncher extends JvmProcess implements BundleContextAware, osgiLocations.append(location); if (log.isTraceEnabled()) log.trace("Added bundle " + name - + " to slc.osgi.locations: " + location); + + " to argeo.osgi.locations: " + location); } else { if (osgiBundles.length() != 0) osgiBundles.append(','); @@ -114,7 +114,7 @@ public class DetachedLauncher extends JvmProcess implements BundleContextAware, getSystemProperties().setProperty("osgi.bundles", osgiBundles.toString()); - getSystemProperties().setProperty("slc.osgi.locations", + getSystemProperties().setProperty("argeo.osgi.locations", osgiLocations.toString()); super.afterPropertiesSet(); diff --git a/runtime/org.argeo.slc.lib.detached/src/main/java/org/argeo/slc/lib/detached/DetachedTestData.java b/runtime/org.argeo.slc.lib.detached/src/main/java/org/argeo/slc/lib/detached/DetachedTestData.java index ee471cce3..05dad5435 100644 --- a/runtime/org.argeo.slc.lib.detached/src/main/java/org/argeo/slc/lib/detached/DetachedTestData.java +++ b/runtime/org.argeo.slc.lib.detached/src/main/java/org/argeo/slc/lib/detached/DetachedTestData.java @@ -2,6 +2,10 @@ package org.argeo.slc.lib.detached; import org.argeo.slc.core.test.context.DefaultContextTestData; +/** + * @deprecated + * + */ public class DetachedTestData extends DefaultContextTestData { } diff --git a/runtime/org.argeo.slc.lib.detached/src/main/java/org/argeo/slc/lib/detached/DetachedTestDefinition.java b/runtime/org.argeo.slc.lib.detached/src/main/java/org/argeo/slc/lib/detached/DetachedTestDefinition.java index 5b691b1a0..11ca2dbb3 100644 --- a/runtime/org.argeo.slc.lib.detached/src/main/java/org/argeo/slc/lib/detached/DetachedTestDefinition.java +++ b/runtime/org.argeo.slc.lib.detached/src/main/java/org/argeo/slc/lib/detached/DetachedTestDefinition.java @@ -10,6 +10,7 @@ import org.argeo.slc.SlcException; import org.argeo.slc.core.structure.tree.TreeSRelatedHelper; import org.argeo.slc.core.test.SimpleResultPart; import org.argeo.slc.core.test.context.ContextUtils; +import org.argeo.slc.core.test.context.DefaultContextTestData; import org.argeo.slc.detached.DetachedAnswer; import org.argeo.slc.detached.DetachedClient; import org.argeo.slc.detached.DetachedRequest; @@ -69,7 +70,7 @@ public class DetachedTestDefinition extends TreeSRelatedHelper implements request.setUuid(UUID.randomUUID().toString()); request.setRef(stepBeanNameT); - DetachedTestData testData = testRun.getTestData(); + DefaultContextTestData testData = testRun.getTestData(); if (testData != null) { Map values = testData.getValues(); Properties inputParameters = new Properties(); @@ -84,26 +85,29 @@ public class DetachedTestDefinition extends TreeSRelatedHelper implements + stepBeanNameT, e); } + DetachedAnswer answer; try { - DetachedAnswer answer = client.receiveAnswer(); - if (answer.getStatus() == DetachedAnswer.ERROR) - throw new SlcException("Error when executing step " - + answer.getUuid() + ": " + answer.getLog()); - else - log.info("Received answer for '" + request.getRef() + "' (" - + answer.getStatusAsString() + "):" + answer.getLog()); - - if (testData != null) { - Properties outputParameters = answer.getProperties(); - for (Object key : outputParameters.keySet()) - testData.getValues().put(key.toString(), - outputParameters.get(key)); - } + answer = client.receiveAnswer(); } catch (Exception e) { throw new SlcException("Could not receive answer #" + request.getUuid() + " for step " + stepBeanNameT, e); + } + + if (answer.getStatus() == DetachedAnswer.ERROR) + throw new SlcException("Error when executing step " + + answer.getUuid() + ": " + answer.getLog()); + else + log.info("Received answer for '" + request.getRef() + "' (" + + answer.getStatusAsString() + "):" + answer.getLog()); + + if (testData != null) { + Properties outputParameters = answer.getProperties(); + for (Object key : outputParameters.keySet()) + testData.getValues().put(key.toString(), + outputParameters.get(key)); } + if (testData != null) { ContextUtils.compareReachedExpected(testData, testRun .getTestResult(), this); -- 2.39.2