@Override
  public void deploy(DeploymentPhaseContext phaseContext) throws DeploymentUnitProcessingException {
    DeploymentUnit deploymentUnit = phaseContext.getDeploymentUnit();

    if (deploymentUnit.hasAttachment(ClojureMetaData.ATTACHMENT_KEY)) {
      return;
    }

    Timer t = new Timer("parsing deployment descriptor");
    String deploymentName = deploymentUnit.getName();

    try {
      VirtualFile descriptor = getDescriptorFile(deploymentUnit);
      if (descriptor == null) {
        return;
      }

      ClojureMetaData appMetaData =
          new ClojureMetaData(deploymentName, ClojureMetaData.parse(descriptor.getPhysicalFile()));

      appMetaData.attachTo(deploymentUnit);

      File root = appMetaData.getRoot();

      if (root == null) {
        throw new DeploymentUnitProcessingException("No application root specified.");
      }

      if (!root.exists()) {
        throw new DeploymentUnitProcessingException(
            "Application root does not exist: " + root.getAbsolutePath());
      }

      VirtualFile virtualRoot = VFS.getChild(root.toURI());
      MountHandle mountHandle = null;

      if (!root.isDirectory()) {
        // Expand the referenced root if it's not a directory (ie .ima archive)
        mountHandle =
            new MountHandle(
                VFS.mountZipExpanded(virtualRoot, virtualRoot, TempFileProviderService.provider()));
      }

      deploymentUnit.putAttachment(
          Attachments.DEPLOYMENT_ROOT, new ResourceRoot(virtualRoot, mountHandle));
      deploymentUnit.putAttachment(ClojureMetaData.DESCRIPTOR_FILE, descriptor.getPhysicalFile());

    } catch (Exception e) {
      throw new DeploymentUnitProcessingException(e);
    }
    t.done();
  }
  private void mount(File file, DeploymentUnit unit, JarMountMap mountMap) throws IOException {
    VirtualFile mountPath =
        VFS.getChild(File.createTempFile(file.getName(), ".jar", tmpMountDir(unit)).toURI());
    log.debug(unit.getName() + ": mounting " + file);
    final ResourceRoot childResource =
        new ResourceRoot(
            mountPath,
            new MountHandle(VFS.mountZip(file, mountPath, TempFileProviderService.provider())));
    ModuleRootMarker.mark(childResource);
    unit.addToAttachmentList(Attachments.RESOURCE_ROOTS, childResource);

    mountMap.put(mountPath.toURL().toExternalForm(), file.toURI().toURL().toExternalForm());
  }
 @Override
 public Closeable mountDeploymentContent(
     final VirtualFile contents, VirtualFile mountPoint, MountType type) throws IOException {
   // according to the javadoc contents can not be null
   assert contents != null : "null contents";
   switch (type) {
     case ZIP:
       return VFS.mountZip(contents, mountPoint, tempFileProvider);
     case EXPANDED:
       return VFS.mountZipExpanded(contents, mountPoint, tempFileProvider);
     case REAL:
       return VFS.mountReal(contents.getPhysicalFile(), mountPoint);
     default:
       throw ServerMessages.MESSAGES.unknownMountType(type);
   }
 }
 public void testSimpleEar() throws Exception {
   System.setProperty("java.naming.factory.initial", MockInitialContextFactory.class.getName());
   VirtualFile ear = VFS.getChild("multiplefiles-top-level.ear");
   createAssembledDirectory(ear).addPath("multiplefiles");
   VFSDeploymentUnit unit = assertDeploy(ear);
   assertNotNull(NonSerializableFactory.lookup("TestContext1"));
   assertNotNull(NonSerializableFactory.lookup("TestContext2"));
   undeploy(unit);
   assertNull(NonSerializableFactory.lookup("TestContext1"));
   assertNull(NonSerializableFactory.lookup("TestContext2"));
 }
 /**
  * Creates a {@link ResourceRoot} for the passed {@link VirtualFile file} and adds it to the list
  * of {@link ResourceRoot}s in the {@link DeploymentUnit deploymentUnit}
  *
  * @param file The file for which the resource root will be created
  * @return Returns the created {@link ResourceRoot}
  * @throws java.io.IOException
  */
 private synchronized ResourceRoot createResourceRoot(final VirtualFile file)
     throws DeploymentUnitProcessingException {
   try {
     final Closeable closable =
         file.isFile() ? VFS.mountZip(file, file, TempFileProviderService.provider()) : null;
     final MountHandle mountHandle = new MountHandle(closable);
     final ResourceRoot resourceRoot = new ResourceRoot(file, mountHandle);
     ModuleRootMarker.mark(resourceRoot);
     ResourceRootIndexer.indexResourceRoot(resourceRoot);
     return resourceRoot;
   } catch (IOException e) {
     throw new RuntimeException(e);
   }
 }
  public void setRoot(String path) {
    if (path != null) {
      String sanitizedPath = null;

      if (path.indexOf("\\\\") >= 0) {
        sanitizedPath = path.replaceAll("\\\\\\\\", "/");
        sanitizedPath = sanitizedPath.replaceAll("\\\\", "");
      } else {
        sanitizedPath = path.replaceAll("\\\\", "/");
      }
      VirtualFile root = VFS.getChild(sanitizedPath);
      setRoot(root);
    }
  }
  @Override
  public Closeable mountDeploymentContent(
      String name,
      String runtimeName,
      byte[] deploymentHash,
      VirtualFile mountPoint,
      boolean mountExpanded)
      throws IOException {
    // Internal deployments have no hash, and are unique by name
    if (deploymentHash == null) {
      File file = new File(systemDeployDir, name);
      return VFS.mountZip(file, mountPoint, tempFileProvider);
    }

    final File external = getExternalFileReference(deploymentHash, false);
    if (external.exists()) {
      final InputStream is = new FileInputStream(external);
      try {
        final BufferedReader reader = new BufferedReader(new InputStreamReader(is));
        final String fileName = reader.readLine();
        final File realRoot = new File(fileName);
        if (!realRoot.exists()) {
          throw new FileNotFoundException(fileName);
        }
        return VFS.mountReal(realRoot, mountPoint);
      } finally {
        safeClose(is);
      }
    }

    final File content = getDeploymentContentFile(deploymentHash);
    if (mountExpanded) {
      return VFS.mountZipExpanded(content, mountPoint, tempFileProvider);
    } else {
      return VFS.mountZip(content, mountPoint, tempFileProvider);
    }
  }
  /** {@inheritDoc} */
  public Collection<String> getPaths() {
    final List<String> index = new ArrayList<String>();
    // First check for an index file
    final VirtualFile indexFile = VFS.getChild(root.getPathName() + ".index");
    if (indexFile.exists()) {
      try {
        final BufferedReader r = new BufferedReader(new InputStreamReader(indexFile.openStream()));
        try {
          String s;
          while ((s = r.readLine()) != null) {
            index.add(s.trim());
          }
          return index;
        } finally {
          // if exception is thrown, undo index creation
          r.close();
        }
      } catch (IOException e) {
        index.clear();
      }
    }

    FilterVirtualFileVisitor visitor =
        new FilterVirtualFileVisitor(
            new VirtualFileFilter() {
              @Override
              public boolean accepts(VirtualFile file) {
                return file.isDirectory();
              }
            },
            VisitorAttributes.RECURSE);
    try {
      root.visit(visitor);
    } catch (IOException e) {
      index.clear();
    }

    index.add("");
    for (VirtualFile dir : visitor.getMatched()) {
      index.add(dir.getPathNameRelativeTo(root));
    }

    return index;
  }
  public void deploy(DeploymentPhaseContext phaseContext) throws DeploymentUnitProcessingException {
    final DeploymentUnit deploymentUnit = phaseContext.getDeploymentUnit();
    final ResourceRoot resourceRoot = deploymentUnit.getAttachment(Attachments.DEPLOYMENT_ROOT);
    if (resourceRoot == null) {
      return;
    }
    final VirtualFile deploymentRoot = resourceRoot.getRoot();
    if (deploymentRoot == null || !deploymentRoot.exists()) {
      return;
    }

    final String deploymentRootName = deploymentRoot.getLowerCaseName();
    if (!deploymentRootName.endsWith(RAR_EXTENSION)) {
      return;
    }
    // we do not load classes from the module resource root
    ModuleRootMarker.mark(resourceRoot, false);

    try {
      final List<VirtualFile> childArchives = deploymentRoot.getChildren(CHILD_ARCHIVE_FILTER);

      for (final VirtualFile child : childArchives) {
        final Closeable closable =
            child.isFile()
                ? VFS.mountZip(child, child, TempFileProviderService.provider())
                : NO_OP_CLOSEABLE;
        final MountHandle mountHandle = new MountHandle(closable);
        final ResourceRoot childResource = new ResourceRoot(child, mountHandle);
        ModuleRootMarker.mark(childResource);
        deploymentUnit.addToAttachmentList(Attachments.RESOURCE_ROOTS, childResource);
        resourceRoot.addToAttachmentList(
            Attachments.INDEX_IGNORE_PATHS, child.getPathNameRelativeTo(deploymentRoot));
      }
    } catch (IOException e) {
      throw new DeploymentUnitProcessingException(
          "Failed to process RA child archives for [" + deploymentRoot + "]", e);
    }
  }
  @SuppressWarnings("unchecked")
  @Override
  public void deploy(DeploymentPhaseContext phaseContext) throws DeploymentUnitProcessingException {
    DeploymentUnit unit = phaseContext.getDeploymentUnit();

    ClojureMetaData metaData = unit.getAttachment(ClojureMetaData.ATTACHMENT_KEY);
    if (metaData == null && !ArchivedDeploymentMarker.isMarked(unit)) {
      return;
    }

    File root = metaData.getRoot();

    JarMountMap mountMap = new JarMountMap();
    unit.putAttachment(JarMountMap.ATTACHMENT_KEY, mountMap);

    try {
      List<File> dependencyJars =
          ApplicationBootstrapUtils.getDependencies(
              root, metaData.resolveDependencies(), metaData.getLeinProfiles());

      boolean clojureProvided = false;

      for (File each : dependencyJars) {
        if (each.getName().matches("^clojure(-\\d.\\d.\\d)?\\.jar$")) {
          clojureProvided = true;
        }
        mount(each, unit, mountMap);
      }

      if (!clojureProvided) {
        Immutant immutant =
            (Immutant)
                phaseContext.getServiceRegistry().getService(CoreServices.IMMUTANT).getValue();

        log.warn(
            "No clojure.jar found within "
                + metaData.getApplicationName()
                + ", Using built-in clojure.jar (v"
                + immutant.getClojureVersion()
                + ")");

        // borrow the shipped clojure.jar
        String jarPath =
            System.getProperty("jboss.home.dir") + "/modules/org/immutant/core/main/clojure.jar";
        mount(new File(jarPath), unit, mountMap);
      }

      // mount the runtime jar
      String runtimePath =
          System.getProperty("jboss.home.dir")
              + "/modules/org/immutant/core/main/immutant-runtime-impl.jar";
      mount(new File(runtimePath), unit, mountMap);

      for (String each : ApplicationBootstrapUtils.resourceDirs(root, metaData.getLeinProfiles())) {
        final ResourceRoot childResource = new ResourceRoot(VFS.getChild(each), null);
        ModuleRootMarker.mark(childResource);
        unit.addToAttachmentList(Attachments.RESOURCE_ROOTS, childResource);
      }

    } catch (Exception e) {
      throw new DeploymentUnitProcessingException(e);
    }

    // disable the annotation index, since we're not an EE app and it spits nasty WARNs to the log
    // if
    // ring is included as an app dep in relation to some jetty classes
    unit.putAttachment(Attachments.COMPUTE_COMPOSITE_ANNOTATION_INDEX, false);
    // AS let's us disable the index, but then assumes it's always there, so we give it an empty one
    unit.putAttachment(
        Attachments.COMPOSITE_ANNOTATION_INDEX, new CompositeIndex(Collections.EMPTY_LIST));
  }
Beispiel #11
0
 @Override
 public VirtualFile getContent(byte[] hash) {
   if (hash == null) throw DeploymentRepositoryMessages.MESSAGES.nullVar("hash");
   return VFS.getChild(getDeploymentContentFile(hash, true).toURI());
 }