/**
  * Return the ConfiguratorConfigFile which is determined by the parameters set in Manipulator.
  *
  * @param manipulator
  * @return File
  */
 private static File getConfigFile(Manipulator manipulator) throws IllegalStateException {
   File fwConfigLoc = manipulator.getLauncherData().getFwConfigLocation();
   File baseDir = null;
   if (fwConfigLoc == null) {
     baseDir = manipulator.getLauncherData().getHome();
     if (baseDir == null) {
       if (manipulator.getLauncherData().getLauncher() != null) {
         baseDir = manipulator.getLauncherData().getLauncher().getParentFile();
       } else {
         throw new IllegalStateException(
             "All of fwConfigFile, home, launcher are not set."); //$NON-NLS-1$
       }
     }
   } else {
     if (fwConfigLoc.exists())
       if (fwConfigLoc.isDirectory()) baseDir = fwConfigLoc;
       else baseDir = fwConfigLoc.getParentFile();
     else {
       // TODO We need to decide whether launcher data configLocation is the location of a file or
       // a directory
       if (fwConfigLoc.getName().endsWith(".ini")) // $NON-NLS-1$
       baseDir = fwConfigLoc.getParentFile();
       else baseDir = fwConfigLoc;
     }
   }
   File configuratorFolder =
       new File(baseDir, SimpleConfiguratorManipulatorImpl.CONFIGURATOR_FOLDER);
   File targetFile = new File(configuratorFolder, SimpleConfiguratorManipulatorImpl.CONFIG_LIST);
   if (!Utils.createParentDir(targetFile)) return null;
   return targetFile;
 }
  private void manipulatePending() {

    if (!pendingManipulationsWithoutAnimation.isEmpty()) {
      animating = true;
      for (final Manipulator manipulator : pendingManipulationsWithoutAnimation) {
        manipulator.manipulate(adapter.adapter);
      }
      pendingManipulationsWithoutAnimation.clear();
      adapter.notifyDataSetChanged();

      post(
          new Runnable() {

            @Override
            public void run() {
              animating = false;
              manipulatePending();
            }
          });
    } else {

      if (pendingManipulations.isEmpty()) {
        return;
      }

      prepareAnimation();

      for (final Manipulator manipulator : pendingManipulations) {
        manipulator.manipulate(adapter.adapter);
      }
      pendingManipulations.clear();

      doAnimation();
    }
  }
  public BundleInfo[] save(Manipulator manipulator, boolean backup) throws IOException {
    List setToInitialConfig = new LinkedList();
    List setToSimpleConfig = new LinkedList();
    ConfigData configData = manipulator.getConfigData();

    if (!divideBundleInfos(
        manipulator,
        setToInitialConfig,
        setToSimpleConfig,
        configData.getInitialBundleStartLevel())) return configData.getBundles();

    File outputFile = getConfigFile(manipulator);
    URI installArea =
        ParserUtils.getOSGiInstallArea(
                Arrays.asList(manipulator.getLauncherData().getProgramArgs()),
                manipulator.getConfigData().getProperties(),
                manipulator.getLauncherData())
            .toURI();
    saveConfiguration(
        (BundleInfo[]) setToSimpleConfig.toArray(new BundleInfo[setToSimpleConfig.size()]),
        outputFile,
        installArea,
        backup);
    configData.setProperty(
        SimpleConfiguratorManipulatorImpl.PROP_KEY_CONFIGURL, outputFile.toURL().toExternalForm());
    return orderingInitialConfig(setToInitialConfig);
  }
  private boolean divideBundleInfos(
      Manipulator manipulator,
      List setToInitialConfig,
      List setToSimpleConfig,
      final int initialBSL) {
    BundlesState state = manipulator.getBundlesState();
    BundleInfo[] targetBundleInfos = null;
    if (state.isFullySupported()) {
      targetBundleInfos = state.getExpectedState();
    } else {
      targetBundleInfos = manipulator.getConfigData().getBundles();
    }
    BundleInfo configuratorBInfo = null;
    for (int i = 0; i < targetBundleInfos.length; i++) {
      if (isTargetConfiguratorBundle(targetBundleInfos[i].getLocation())) {
        if (targetBundleInfos[i].isMarkedAsStarted()) {
          configuratorBInfo = targetBundleInfos[i];
          break;
        }
      }
    }
    if (configuratorBInfo == null && !manipulators.contains(manipulator)) {
      return false;
    } else if (manipulators.contains(manipulator) && targetBundleInfos.length == 0) {
      // Resulting state will have no bundles - so is an uninstall, including
      // uninstall of the configurator. However, we have seen this manipulator
      // before with a target configurator bundle, so allow uninstall to proceed,
      // but only get one chance.
      manipulators.remove(manipulator);
    } else if (!manipulators.contains(manipulator)) {
      manipulators.add(manipulator);
    }

    if (state.isFullySupported()) {
      state.resolve(false);
    }

    LocationInfo info = new LocationInfo();
    setSystemBundles(state, info);
    if (configuratorBInfo != null) {
      setPrerequisiteBundles(configuratorBInfo, state, info);
      SortedMap bslToList = getSortedMap(initialBSL, targetBundleInfos);
      algorithm(
          initialBSL, bslToList, configuratorBInfo, setToInitialConfig, setToSimpleConfig, info);
    }
    return true;
  }
 public <T extends ListAdapter> void manipulateWithoutAnimation(final Manipulator<T> manipulator) {
   if (!animating) {
     manipulator.manipulate((T) adapter.adapter);
     adapter.notifyDataSetChanged();
   } else {
     pendingManipulationsWithoutAnimation.add(manipulator);
   }
 }
  public <T extends ListAdapter> void manipulate(final Manipulator<T> manipulator) {
    if (!animating) {
      prepareAnimation();

      manipulator.manipulate((T) adapter.adapter);

      doAnimation();
    } else {
      pendingManipulations.add(manipulator);
    }
  }
Beispiel #7
0
public class TourApp_122 implements context.arch.handler.Handler {

  context.test.contextIntensity.Manipulator manu = Manipulator.getInstance();

  public static final int DEFAULT_PORT = 5555;

  public static final java.lang.String SUBSCRIBER_ID = "Tour";

  private int port;

  private java.lang.String userid;

  private context.apps.Tour.DemoFile demo;

  private context.arch.BaseObject server;

  private java.lang.String intId;

  private java.lang.String intHost;

  private int intPort;

  private java.lang.String name;

  private java.lang.String affiliation;

  private java.lang.String email;

  private java.lang.String interests;

  private java.lang.Long initialTime;

  private context.apps.Tour.DemoInterests demoInterests;

  private context.apps.Tour.DemoVisits demoVisits;

  public final java.lang.String NO_INTEREST = "noInterest";

  public TourApp_122(
      java.lang.String userid, java.lang.String configFile, java.lang.String demoFile) {
    this(DEFAULT_PORT, userid, configFile, demoFile);
  }

  public TourApp_122(
      int localport,
      java.lang.String userid,
      java.lang.String configFile,
      java.lang.String demoFile) {
    try {
      server = new context.arch.BaseObject(localport);
      port = localport;
      this.userid = userid;
      demo = new context.apps.Tour.DemoFile(demoFile);
      context.arch.util.XMLURLReader reader = new context.arch.util.XMLURLReader(configFile);
      context.arch.comm.DataObject data = reader.getParsedData();
      context.arch.util.Configuration config = new context.arch.util.Configuration(data);
      context.arch.util.ConfigObjects interpreters = config.getInterpreterConfigurations();
      if (interpreters != null) {
        context.arch.util.ConfigObject interpreter =
            (context.arch.util.ConfigObject) interpreters.getEnumeration().nextElement();
        intId = interpreter.getId();
        intHost = interpreter.getHost();
        intPort = Integer.parseInt(interpreter.getPort());
      }
      context.arch.util.ConfigObjects servers = config.getServerConfigurations();
      if (servers != null) {
        for (java.util.Enumeration e = servers.getEnumeration(); e.hasMoreElements(); ) {
          context.arch.util.ConfigObject serverObj =
              (context.arch.util.ConfigObject) e.nextElement();
          if (serverObj.getId().indexOf(userid) != -1) {
            java.lang.String serverId = serverObj.getId();
            java.lang.String serverHost = serverObj.getHost();
            int serverPort = Integer.parseInt(serverObj.getPort());
            context.arch.comm.DataObject callbacks =
                server.getWidgetCallbacks(serverHost, serverPort, serverId);
            java.lang.String callbacksError = (new context.arch.util.Error(callbacks)).getError();
            context.arch.subscriber.Callbacks calls = null;
            if (callbacksError.equals(Error.NO_ERROR)) {
              calls = new context.arch.subscriber.Callbacks(callbacks);
            }
            for (int i = 0; i < calls.numCallbacks(); i++) {
              context.arch.subscriber.Callback callback = calls.getCallbackAt(i);
              if (callback.getName().indexOf(WTourRegistration.UPDATE) != -1) {
                context.arch.util.Error error =
                    server.subscribeTo(
                        this,
                        port,
                        SUBSCRIBER_ID,
                        serverHost,
                        serverPort,
                        serverId,
                        callback.getName(),
                        SUBSCRIBER_ID + Constants.SPACER + WTourRegistration.UPDATE);
              } else {
                if (callback.getName().indexOf(WTourDemo.VISIT) != -1) {
                  context.arch.util.Error error =
                      server.subscribeTo(
                          this,
                          port,
                          SUBSCRIBER_ID,
                          serverHost,
                          serverPort,
                          serverId,
                          callback.getName(),
                          SUBSCRIBER_ID + Constants.SPACER + WTourDemo.VISIT);
                } else {
                  if (callback.getName().indexOf(WTourDemo.INTEREST) != -1) {
                    context.arch.util.Error error =
                        server.subscribeTo(
                            this,
                            port,
                            SUBSCRIBER_ID,
                            serverHost,
                            serverPort,
                            serverId,
                            callback.getName(),
                            SUBSCRIBER_ID + Constants.SPACER + WTourDemo.INTEREST);
                  } else {
                    if (callback.getName().indexOf(WTourEnd.END) != -1) {
                      context.arch.util.Error error =
                          server.subscribeTo(
                              this,
                              port,
                              SUBSCRIBER_ID,
                              serverHost,
                              serverPort,
                              serverId,
                              callback.getName(),
                              SUBSCRIBER_ID + Constants.SPACER + WTourEnd.END);
                    }
                  }
                }
              }
            }
          }
        }
      }
    } catch (java.net.MalformedURLException mue) {
      System.out.println("TourApp_122 MalformedURL: " + mue);
    } catch (context.arch.comm.language.DecodeException de) {
      System.out.println("TourApp_122 Decode: " + de);
    }
  }

  public context.arch.comm.DataObject handle(
      java.lang.String callback, context.arch.comm.DataObject data)
      throws context.arch.InvalidMethodException, context.arch.MethodException {
    if (callback.equals(SUBSCRIBER_ID + Constants.SPACER + WTourRegistration.UPDATE)) {
      demoInterests = new context.apps.Tour.DemoInterests();
      demoVisits = new context.apps.Tour.DemoVisits();
      context.arch.storage.AttributeNameValues atts =
          new context.arch.storage.AttributeNameValues(data);
      manu.enterScheduler(WTourRegistration.UPDATE, "1");
      interests =
          (java.lang.String) atts.getAttributeNameValue(WTourRegistration.INTERESTS).getValue();
      manu.exitScheduler(WTourRegistration.UPDATE, "1");
      context.arch.storage.AttributeNameValue att =
          atts.getAttributeNameValue(WTourRegistration.CONTACT_INFO);
      context.arch.storage.AttributeNameValues atts2 =
          (context.arch.storage.AttributeNameValues) att.getValue();
      manu.enterScheduler(WTourRegistration.UPDATE, "2");
      name = (java.lang.String) atts2.getAttributeNameValue(WTourRegistration.NAME).getValue();
      manu.exitScheduler(WTourRegistration.UPDATE, "2");
      affiliation =
          (java.lang.String) atts2.getAttributeNameValue(WTourRegistration.AFFILIATION).getValue();
      email = (java.lang.String) atts2.getAttributeNameValue(WTourRegistration.EMAIL).getValue();
      initialTime =
          new java.lang.Long(
              (java.lang.String)
                  atts.getAttributeNameValue(WTourRegistration.TIMESTAMP).getValue());
      context.arch.storage.AttributeNameValues input =
          new context.arch.storage.AttributeNameValues();
      input.addAttributeNameValue(WTourRegistration.INTERESTS, interests);
      input.addAttributeNameValue(
          Demos.DEMOS, demo.getDemos().toAttributeNameValues(), Attribute.STRUCT);
      java.lang.String result = askInterpreter(input);
    } else {
      if (callback.equals(SUBSCRIBER_ID + Constants.SPACER + WTourDemo.VISIT)) {
        manu.enterScheduler(WTourDemo.VISIT, "3");
        context.arch.storage.AttributeNameValues atts =
            new context.arch.storage.AttributeNameValues(data);
        manu.exitScheduler(WTourDemo.VISIT, "3");
        java.lang.String demourl =
            (java.lang.String) atts.getAttributeNameValue(WTourDemo.DEMO_URL).getValue();
        java.lang.String demoerurl =
            (java.lang.String) atts.getAttributeNameValue(WTourDemo.DEMOER_URL).getValue();
        manu.enterScheduler(WTourDemo.VISIT, "4");
        java.lang.String demoName =
            (java.lang.String) atts.getAttributeNameValue(WTourDemo.DEMO_NAME).getValue();
        manu.exitScheduler(WTourDemo.VISIT, "4");
        java.lang.String timestamp =
            (java.lang.String) atts.getAttributeNameValue(WTourDemo.TIMESTAMP).getValue();
        context.apps.Tour.DemoInterest demoInterest =
            new context.apps.Tour.DemoInterest(demoName, NO_INTEREST);
        demoInterests.addDemoInterest(demoInterest);
        demoVisits.addDemoVisit(demoName, demourl, demoerurl, timestamp);
        context.arch.storage.AttributeNameValues input =
            new context.arch.storage.AttributeNameValues();
        input.addAttributeNameValue(
            DemoInterests.DEMO_INTERESTS, demoInterests.toAttributeNameValues(), Attribute.STRUCT);
        input.addAttributeNameValue(WTourRegistration.INTERESTS, interests);
        input.addAttributeNameValue(
            Demos.DEMOS, demo.getDemos().toAttributeNameValues(), Attribute.STRUCT);
        java.lang.String result = askInterpreter(input);
      } else {
        if (callback.equals(SUBSCRIBER_ID + Constants.SPACER + WTourDemo.INTEREST)) {
          context.arch.storage.AttributeNameValues atts =
              new context.arch.storage.AttributeNameValues(data);
          manu.enterScheduler(WTourDemo.INTEREST, "5");
          java.lang.String interest =
              (java.lang.String) atts.getAttributeNameValue(WTourDemo.INTEREST_LEVEL).getValue();
          manu.exitScheduler(WTourDemo.INTEREST, "5");
          java.lang.String demoName =
              (java.lang.String) atts.getAttributeNameValue(WTourDemo.DEMO_NAME).getValue();
          manu.enterScheduler(WTourDemo.INTEREST, "6");
          demoInterests.addDemoInterest(demoName, interest);
          manu.exitScheduler(WTourDemo.INTEREST, "6");
          context.arch.storage.AttributeNameValues input =
              new context.arch.storage.AttributeNameValues();
          input.addAttributeNameValue(
              DemoInterests.DEMO_INTERESTS,
              demoInterests.toAttributeNameValues(),
              Attribute.STRUCT);
          input.addAttributeNameValue(WTourRegistration.INTERESTS, interests);
          input.addAttributeNameValue(
              Demos.DEMOS, demo.getDemos().toAttributeNameValues(), Attribute.STRUCT);
          java.lang.String result = askInterpreter(input);
          context.apps.Tour.DemoVisit dv = demoVisits.getDemoVisit(demoName);
          if (dv != null) {
            dv.setInterest(interest);
          }
        } else {
          if (callback.equals(SUBSCRIBER_ID + Constants.SPACER + WTourEnd.END)) {
            manu.enterScheduler(WTourEnd.END, "7");
            context.arch.storage.AttributeNameValues input =
                new context.arch.storage.AttributeNameValues();
            manu.exitScheduler(WTourEnd.END, "7");
            input.addAttributeNameValue(
                DemoInterests.DEMO_INTERESTS,
                demoInterests.toAttributeNameValues(),
                Attribute.STRUCT);
            input.addAttributeNameValue(WTourRegistration.INTERESTS, interests);
            input.addAttributeNameValue(
                Demos.DEMOS, demo.getDemos().toAttributeNameValues(), Attribute.STRUCT);
            manu.enterScheduler(WTourEnd.END, "8");
            java.lang.String result = askInterpreter(input);
            manu.exitScheduler(WTourEnd.END, "8");
            java.lang.StringBuffer message =
                new java.lang.StringBuffer(
                    name + ", thank you for visiting the FCL lab and taking our tour!\n\n");
            message.append("Following is a summary of your tour:\n\n");
            for (int i = 0; i < demoVisits.numDemoVisits(); i++) {
              context.apps.Tour.DemoVisit dv = demoVisits.getDemoVisitAt(i);
              message.append(
                  "At "
                      + (new java.util.Date(dv.getTime())).toString()
                      + ", you visited the "
                      + dv.getDemoName()
                      + " demo.\n");
              message.append(
                  "Your level of interest in this demo was: " + dv.getInterest() + ".\n");
              message.append(
                  "If you would like more information on this demo, please go to the demo's web page at\n");
              message.append(
                  dv.getDemoUrl()
                      + ", or go the demoer's web page at "
                      + dv.getDemoerUrl()
                      + ".\n\n");
            }
            message.append(
                "Based on your interests which you used to register with the tour guide program, we think\n");
            message.append("the following demos might also be interesting to you: \n" + result);
            context.arch.util.SendMail sm = new context.arch.util.SendMail();
            sm.sendMail(
                server.getHostAddress(),
                "study.cs.hku.hk",
                "*****@*****.**",
                email,
                "FCL Demo Trip Summary",
                message.toString());
          } else {
            throw new context.arch.InvalidMethodException(Error.UNKNOWN_CALLBACK_ERROR);
          }
        }
      }
    }
    return null;
  }

  private java.lang.String askInterpreter(context.arch.storage.AttributeNameValues input) {
    context.arch.comm.DataObject result = server.askInterpreter(intHost, intPort, intId, input);
    context.arch.storage.AttributeNameValues atts =
        new context.arch.storage.AttributeNameValues(result);
    if (atts == null) {
      return null;
    } else {
      if (atts.numAttributeNameValues() == 0) {
        return new java.lang.String("There are no more demos to recommend.");
      }
    }
    java.lang.StringBuffer sb = new java.lang.StringBuffer();
    for (int i = 0; i++ < atts.numAttributeNameValues(); i++) {
      sb.append(atts.getAttributeNameValueAt(i).getValue() + ", ");
    }
    return sb.toString();
  }

  public void quit() {
    this.server.quit();
  }
}
 public static void main(String[] args) {
   HasF hf = new HasF();
   Manipulator<HasF> manipulator = new Manipulator<HasF>(hf);
   manipulator.manipulate();
 }
  public void updateBundles(Manipulator manipulator) throws IOException {
    if (DEBUG)
      System.out.println("SimpleConfiguratorManipulatorImpl#updateBundles()"); // $NON-NLS-1$

    BundlesState bundleState = manipulator.getBundlesState();

    if (bundleState == null) return;
    if (bundleState.isFullySupported()) bundleState.resolve(true);

    BundleInfo[] currentBInfos = bundleState.getExpectedState();
    if (!isTargetConfiguratorBundle(currentBInfos)) return;
    Properties properties = new Properties();
    String[] jvmArgs = manipulator.getLauncherData().getJvmArgs();
    for (int i = 0; i < jvmArgs.length; i++) {
      if (jvmArgs[i].startsWith("-D")) { // $NON-NLS-1$
        int index = jvmArgs[i].indexOf("="); // $NON-NLS-1$
        if (index > 0 && jvmArgs[i].length() > 2) {
          String key = jvmArgs[i].substring(2, index);
          String value = jvmArgs[i].substring(index + 1);
          properties.setProperty(key, value);
        }
      }
    }

    Utils.appendProperties(properties, manipulator.getConfigData().getProperties());
    boolean exclusiveInstallation =
        Boolean.valueOf(
                properties.getProperty(
                    SimpleConfiguratorManipulatorImpl.PROP_KEY_EXCLUSIVE_INSTALLATION))
            .booleanValue();
    File configFile = getConfigFile(manipulator);

    File installArea =
        ParserUtils.getOSGiInstallArea(
            Arrays.asList(manipulator.getLauncherData().getProgramArgs()),
            manipulator.getConfigData().getProperties(),
            manipulator.getLauncherData());
    BundleInfo[] toInstall = new BundleInfo[0];

    boolean isShared = isSharedInstallSetup(installArea, configFile);
    if (!isShared
        || (isShared && !hasBaseChanged(installArea.toURI(), configFile.getParentFile()))) {
      try {
        // input stream will be closed for us
        toInstall = loadConfiguration(new FileInputStream(configFile), installArea.toURI());
      } catch (FileNotFoundException e) {
        // no file, just return an empty list
        toInstall = new BundleInfo[0];
      }
    }

    List toUninstall = new LinkedList();
    if (exclusiveInstallation)
      for (int i = 0; i < currentBInfos.length; i++) {
        boolean install = false;
        for (int j = 0; j < toInstall.length; j++)
          if (currentBInfos[i].getLocation().equals(toInstall[j].getLocation())) {
            install = true;
            break;
          }
        if (!install) toUninstall.add(currentBInfos[i]);
      }

    for (int i = 0; i < toInstall.length; i++) {
      try {
        bundleState.installBundle(toInstall[i]);
      } catch (RuntimeException e) {
        // Ignore
      }
    }
    if (exclusiveInstallation)
      for (Iterator ite = toUninstall.iterator(); ite.hasNext(); ) {
        BundleInfo bInfo = (BundleInfo) ite.next();
        bundleState.uninstallBundle(bInfo);
      }

    bundleState.resolve(true);
    manipulator.getConfigData().setBundles(bundleState.getExpectedState());
  }