/**
     * Adds a DAO register under a specified name
     *
     * @param name the name of the DAO
     * @param dao the DAO
     * @return the builder
     */
    public Builder<T> put(String name, DaoRegister<T> daoRegister) {
      AssertArgument.isNotNull(name, "name");
      AssertArgument.isNotNull(daoRegister, "daoRegister");

      map.put(name, daoRegister);

      return this;
    }
Example #2
0
  /**
   * Add the specified classpath resource as an entry in the deployment.
   *
   * @param path The target path of the entry when added to the archive.
   * @param resource The classpath resource.
   * @return This archive instance.
   * @throws java.io.IOException Failed to read resource from classpath.
   */
  public Archive addClasspathResourceEntry(String path, String resource) throws IOException {
    AssertArgument.isNotNull(path, "path");
    AssertArgument.isNotNull(resource, "resource");

    InputStream resourceStream = getClass().getResourceAsStream(resource);
    if (resourceStream == null) {
      throw new IOException("Classpath resource '" + resource + "' no found.");
    } else {
      addEntry(path, resourceStream);
    }

    return this;
  }
  /* (non-Javadoc)
   * @see org.milyn.scribe.DAO#persist(java.lang.Object)
   */
  public Object insert(final Object entity) {
    AssertArgument.isNotNull(entity, "entity");

    entityManager.persist(entity);

    return null;
  }
Example #4
0
  /**
   * Output the entries to the specified output folder on the file system.
   *
   * @param outputFolder The target output folder.
   * @throws java.io.IOException Write failure.
   */
  public void toFileSystem(File outputFolder) throws IOException {
    AssertArgument.isNotNull(outputFolder, "outputFolder");

    if (outputFolder.isFile()) {
      throw new IOException(
          "Cannot write Archive entries to '"
              + outputFolder.getAbsolutePath()
              + "'.  This is a normal file i.e. not a directory.");
    }
    if (!outputFolder.exists()) {
      outputFolder.mkdirs();
    }

    Set<Map.Entry<String, File>> entrySet = entries.entrySet();
    for (Map.Entry<String, File> entry : entrySet) {
      File archEntryFile = entry.getValue();
      byte[] fileBytes;
      File entryFile = new File(outputFolder, entry.getKey());

      if (archEntryFile != null) {
        fileBytes = FileUtils.readFile(archEntryFile);
        entryFile.getParentFile().mkdirs();
        FileUtils.writeFile(fileBytes, entryFile);
      } else {
        entryFile.mkdirs();
      }
    }
  }
Example #5
0
  /**
   * Add the entries from the supplied {@link ZipInputStream} to this archive instance.
   *
   * @param zipStream The zip stream.
   * @return This archive instance.
   * @throws IOException Error reading zip stream.
   */
  public Archive addEntries(ZipInputStream zipStream) throws IOException {
    AssertArgument.isNotNull(zipStream, "zipStream");

    try {
      ZipEntry zipEntry = zipStream.getNextEntry();
      ByteArrayOutputStream outByteStream = new ByteArrayOutputStream();
      byte[] byteReadBuffer = new byte[512];
      int byteReadCount;

      while (zipEntry != null) {
        if (zipEntry.isDirectory()) {
          addEntry(zipEntry.getName(), (byte[]) null);
        } else {
          while ((byteReadCount = zipStream.read(byteReadBuffer)) != -1) {
            outByteStream.write(byteReadBuffer, 0, byteReadCount);
          }
          addEntry(zipEntry.getName(), outByteStream.toByteArray());
          outByteStream.reset();
        }
        zipEntry = zipStream.getNextEntry();
      }
    } finally {
      try {
        zipStream.close();
      } catch (IOException e) {
        logger.debug("Unexpected error closing EDI Mapping Model Zip stream.", e);
      }
    }

    return this;
  }
    /**
     * Copies all of the mappings for the specified map to this builder
     *
     * @param map mapping to be stored in this builder
     * @return the builder
     */
    public Builder<T> putAll(Map<String, ? extends DaoRegister<T>> map) {
      AssertArgument.isNotNull(map, "map");

      this.map.putAll(map);

      return this;
    }
  /* (non-Javadoc)
   * @see org.milyn.scribe.Dao#delete(java.lang.Object)
   */
  public Object delete(Object entity) {
    AssertArgument.isNotNull(entity, "entity");

    entityManager.remove(entity);

    return null;
  }
Example #8
0
  /**
   * Add the supplied class as an entry in the deployment.
   *
   * @param clazz The class to be added.
   * @return This archive instance.
   * @throws java.io.IOException Failed to read class from classpath.
   */
  public Archive addEntry(Class<?> clazz) throws IOException {
    AssertArgument.isNotNull(clazz, "clazz");
    String className = clazz.getName();

    className = className.replace('.', '/') + ".class";
    addClasspathResourceEntry(className, "/" + className);

    return this;
  }
  /* (non-Javadoc)
   * @see org.milyn.scribe.Finder#findBy(java.lang.String, java.util.Map)
   */
  @SuppressWarnings("unchecked")
  public Collection<Object> lookup(final String name, final Map<String, ?> parameters) {
    AssertArgument.isNotNullAndNotEmpty(name, "name");
    AssertArgument.isNotNull(parameters, "parameters");

    final Query emQuery = entityManager.createNamedQuery(name);

    for (final String key : parameters.keySet()) {

      emQuery.setParameter(key, parameters.get(key));
    }
    return emQuery.getResultList();
  }
  /* (non-Javadoc)
   * @see org.milyn.scribe.Finder#findBy(java.lang.String, java.util.Map)
   */
  @SuppressWarnings("unchecked")
  public Collection<Object> lookup(final String name, final Object... parameters) {
    AssertArgument.isNotNullAndNotEmpty(name, "name");
    AssertArgument.isNotNull(parameters, "parameters");

    final Query emQuery = entityManager.createNamedQuery(name);

    for (int i = 0; i < parameters.length; i++) {

      emQuery.setParameter(i + 1, parameters[i]);
    }

    return emQuery.getResultList();
  }
Example #11
0
  /**
   * Add the supplied character data as an entry in the deployment.
   *
   * @param path The target path of the entry when added to the archive.
   * @param data The data.
   * @return This archive instance.
   */
  public Archive addEntry(String path, String data) {
    AssertArgument.isNotNullAndNotEmpty(path, "path");
    AssertArgument.isNotNull(data, "data");

    try {
      addEntry(trimLeadingSlash(path.trim()), data.getBytes("UTF-8"));
    } catch (UnsupportedEncodingException e) {
      throw new RuntimeException(
          "Unexpected UnsupportedEncodingException exception for encoding 'UTF-8' when writing Archive entry from a StringBuilder instance.",
          e);
    }

    return this;
  }
Example #12
0
  /**
   * Add the supplied data as an entry in the deployment.
   *
   * @param path The target path of the entry when added to the archive.
   * @param data The data.
   * @return This archive instance.
   * @throws IOException Error reading from data stream.
   */
  public Archive addEntry(String path, InputStream data) throws IOException {
    AssertArgument.isNotNullAndNotEmpty(path, "path");
    AssertArgument.isNotNull(data, "data");

    try {
      byte[] dataBytes = StreamUtils.readStream(data);
      addEntry(trimLeadingSlash(path.trim()), dataBytes);
    } finally {
      try {
        data.close();
      } catch (IOException e) {
        logger.warn("Unable to close input stream for archive entry '" + path + "'.");
      }
    }

    return this;
  }
Example #13
0
  /**
   * Create an archive of the specified name and containing entries for the data contained in the
   * streams supplied entries arg. specifying the entry name and the value is a InputStream
   * containing the entry data.
   *
   * @param archiveStream The archive output stream.
   * @throws java.io.IOException Write failure.
   */
  public void toOutputStream(ZipOutputStream archiveStream) throws IOException {
    AssertArgument.isNotNull(archiveStream, "archiveStream");

    try {
      writeEntriesToArchive(archiveStream);
    } finally {
      try {
        archiveStream.flush();
      } finally {
        try {
          archiveStream.close();
        } catch (IOException e) {
          logger.info("Unable to close archive output stream.");
        }
      }
    }
  }
  /* (non-Javadoc)
   * @see org.milyn.scribe.QueryFinder#findByQuery(java.lang.String, java.util.Map)
   */
  @SuppressWarnings("unchecked")
  public Collection<Object> lookupByQuery(final String query, final Map<String, ?> parameters) {
    AssertArgument.isNotNullAndNotEmpty(query, "query");
    AssertArgument.isNotNull(parameters, "parameters");

    // Is this useful?
    if (query.startsWith("@")) {
      return lookup(query.substring(1), parameters);
    }

    final Query emQuery = entityManager.createQuery(query);

    for (final String key : parameters.keySet()) {

      emQuery.setParameter(key, parameters.get(key));
    }
    return emQuery.getResultList();
  }
  /* (non-Javadoc)
   * @see org.milyn.scribe.QueryFinder#findByQuery(java.lang.String, java.util.List)
   */
  @SuppressWarnings("unchecked")
  public Collection<Object> lookupByQuery(final String query, final Object... parameters) {
    AssertArgument.isNotNullAndNotEmpty(query, "query");
    AssertArgument.isNotNull(parameters, "parameters");

    // Is this useful?
    if (query.startsWith("@")) {
      return lookup(query.substring(1), parameters);
    }

    final Query emQuery = entityManager.createQuery(query);

    for (int i = 0; i < parameters.length; i++) {

      emQuery.setParameter(i + 1, parameters[i]);
    }

    return emQuery.getResultList();
  }
Example #16
0
  public XsdDOMValidator(Document document) throws SAXException {
    AssertArgument.isNotNull(document, "document");
    this.document = document;

    // Get the default namespace...
    String defaultNamespaceString = getDefaultNamespace(document.getDocumentElement());
    if (defaultNamespaceString != null) {
      try {
        defaultNamespace = new URI(defaultNamespaceString);
      } catch (URISyntaxException e) {
        throw new SAXException(
            "Cannot validate this document with this class.  Namespaces must be valid URIs.  Default Namespace: '"
                + defaultNamespaceString
                + "'.",
            e);
      }
    }

    // Get the full namespace list...
    gatherNamespaces(document.getDocumentElement(), namespaces);
  }
    /** Creates an Builder and copies all of the mappings for the specified map to this builder */
    public Builder(Map<String, ? extends DaoRegister<T>> map) {
      AssertArgument.isNotNull(map, "map");

      this.map = new HashMap<String, DaoRegister<T>>(map);
    }
Example #18
0
 /**
  * Public constructor.
  *
  * @param archiveName The archive name of the deployment.
  */
 public Archive(String archiveName) {
   AssertArgument.isNotNull(archiveName, "archiveName");
   this.archiveName = archiveName;
   createTempDir();
 }
  /**
   * Write the interchange to the specified writer.
   *
   * @param writer The target writer.
   * @param delimiters The delimiters.
   * @throws IOException Error writing interchange.
   */
  public void write(Writer writer, Delimiters delimiters) throws IOException {
    AssertArgument.isNotNull(writer, "writer");

    if (delimiters != null && delimiters != UNEdifactInterchangeParser.defaultUNEdifactDelimiters) {
      // Write a UNA segment definition...
      writer.append("UNA");
      writer.append(delimiters.getComponent());
      writer.append(delimiters.getField());
      writer.append(delimiters.getDecimalSeparator());
      writer.append(delimiters.getEscape());
      writer.append(" ");
      writer.append(delimiters.getSegment());
    } else {
      delimiters = UNEdifactInterchangeParser.defaultUNEdifactDelimiters;
    }

    if (interchangeHeader != null) {
      interchangeHeader.write(writer, delimiters);
    }

    UNEdifactMessage41 previousMessage = null;
    if (messages != null) {
      for (UNEdifactMessage41 message : messages) {

        Object messageObject = message.getMessage();
        if (messageObject == null) {
          throw new IOException("Cannot write null EDI message object.");
        } else if (!(messageObject instanceof EDIWritable)) {
          throw new IOException(
              "Cannot write EDI message object type '"
                  + messageObject.getClass().getName()
                  + "'.  Type must implement '"
                  + EDIWritable.class.getName()
                  + "'.");
        }

        // Write group info...
        if (message.getGroupHeader() != null) {
          if (previousMessage == null) {
            // Start new group..
            message.getGroupHeader().write(writer, delimiters);
          } else if (message.getGroupHeader() != previousMessage.getGroupHeader()) {
            if (previousMessage.getGroupHeader() != null) {
              // Close out previous group...
              previousMessage.getGroupTrailer().write(writer, delimiters);
            }
            // Start new group..
            message.getGroupHeader().write(writer, delimiters);
          } else {
            // The message is part of the same group as the previous message...
          }
        } else if (previousMessage != null && previousMessage.getGroupHeader() != null) {
          // Close out previous group...
          previousMessage.getGroupTrailer().write(writer, delimiters);
        }

        // Write the message...
        if (message.getMessageHeader() != null) {
          message.getMessageHeader().write(writer, delimiters);
        }
        ((EDIWritable) messageObject).write(writer, delimiters);
        if (message.getMessageTrailer() != null) {
          message.getMessageTrailer().write(writer, delimiters);
        }

        // Capture a ref to the message so its group info can be checked
        // against the next message, or closed if it's the last message...
        previousMessage = message;
      }
    }

    // Close out the group of the last message in the interchange (if it's in a group)...
    if (previousMessage != null && previousMessage.getGroupTrailer() != null) {
      // Close out previous group...
      previousMessage.getGroupTrailer().write(writer, delimiters);
    }

    if (interchangeTrailer != null) {
      interchangeTrailer.write(writer, delimiters);
    }

    writer.flush();
  }
  /**
   * Creates a new {@link MultiDaoRegister} and fills it with the provided map.
   *
   * @param <T> the type of the DAO
   * @param map the map that fills the new {@link MultiDaoRegister}
   * @return the new {@link MultiDaoRegister}
   */
  public static <T> MultiDaoRegister<T> newInstance(Map<String, ? extends DaoRegister<T>> map) {
    AssertArgument.isNotNull(map, "map");

    return new MultiDaoRegister<T>(new HashMap<String, DaoRegister<T>>(map));
  }
  /* (non-Javadoc)
   * @see org.milyn.scribe.DAO#merge(java.lang.Object)
   */
  public Object update(final Object entity) {
    AssertArgument.isNotNull(entity, "entity");

    return entityManager.merge(entity);
  }