コード例 #1
0
ファイル: Shell.java プロジェクト: penSecIT/commons-vfs
  /** Does an 'ls' command. */
  private void ls(final String[] cmd) throws FileSystemException {
    int pos = 1;
    final boolean recursive;
    if (cmd.length > pos && cmd[pos].equals("-R")) {
      recursive = true;
      pos++;
    } else {
      recursive = false;
    }

    final FileObject file;
    if (cmd.length > pos) {
      file = mgr.resolveFile(cwd, cmd[pos]);
    } else {
      file = cwd;
    }

    if (file.getType() == FileType.FOLDER) {
      // List the contents
      System.out.println("Contents of " + file.getName());
      listChildren(file, recursive, "");
    } else {
      // Stat the file
      System.out.println(file.getName());
      final FileContent content = file.getContent();
      System.out.println("Size: " + content.getSize() + " bytes.");
      final DateFormat dateFormat =
          DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM);
      final String lastMod = dateFormat.format(new Date(content.getLastModifiedTime()));
      System.out.println("Last modified: " + lastMod);
    }
  }
コード例 #2
0
 public boolean validateLoginData() throws FileSystemException {
   if (loginData == null) {
     return true;
   }
   final FileObject vfsConnection = PublishUtil.createVFSConnection(VFS.getManager(), loginData);
   try {
     final FileSystem fileSystem = vfsConnection.getFileSystem();
     if (fileSystem instanceof WebSolutionFileSystem) {
       final WebSolutionFileSystem webSolutionFileSystem = (WebSolutionFileSystem) fileSystem;
       final Long l =
           (Long)
               webSolutionFileSystem.getAttribute(
                   WebSolutionFileSystem.LAST_REFRESH_TIME_ATTRIBUTE);
       if (l != null) {
         if ((System.currentTimeMillis() - l) > 500) {
           webSolutionFileSystem.getLocalFileModel().refresh();
         }
       }
       return true;
     }
   } catch (FileSystemException fse) {
     // not all file systems support attributes ..
   } catch (IOException e) {
     return false;
   }
   final FileType type = vfsConnection.getType();
   if (type != FileType.FOLDER) {
     return false;
   }
   return true;
 }
コード例 #3
0
  /**
   * Create a ClassLoader to load resources for a {@code HadoopConfiguration}.
   *
   * @param root Configuration root directory
   * @param parent Parent class loader to delegate to if resources cannot be found in the
   *     configuration's directory or provided classpath
   * @param classpathUrls Additional URLs to add to the class loader. These will be added before any
   *     internal resources.
   * @param ignoredClasses Classes (or packages) that should not be loaded by the class loader
   * @return A class loader capable of loading a Hadoop configuration located at {@code root}.
   * @throws ConfigurationException Error creating a class loader for the Hadoop configuration
   *     located at {@code root}
   */
  protected ClassLoader createConfigurationLoader(
      FileObject root,
      ClassLoader parent,
      List<URL> classpathUrls,
      ShimProperties configurationProperties,
      String... ignoredClasses)
      throws ConfigurationException {
    try {
      if (root == null || !FileType.FOLDER.equals(root.getType())) {
        throw new IllegalArgumentException("root must be a folder: " + root);
      }

      // Find all jar files in the configuration, at most 2 folders deep
      List<URL> jars =
          findJarsIn(root, 3, configurationProperties.getConfigSet(SHIM_CLASSPATH_IGNORE));

      // Add the root of the configuration
      jars.add(0, new URL(root.getURL().toExternalForm() + "/"));
      // Inject any overriding URLs before all other paths
      if (classpathUrls != null) {
        jars.addAll(0, classpathUrls);
      }

      return new HadoopConfigurationClassLoader(
          jars.toArray(EMPTY_URL_ARRAY), parent, ignoredClasses);
    } catch (Exception ex) {
      throw new ConfigurationException(
          BaseMessages.getString(PKG, "Error.CreatingClassLoader"), ex);
    }
  }
コード例 #4
0
 /**
  * Parse a set of URLs from a comma-separated list of URLs. If the URL points to a directory all
  * jar files within that directory will be returned as well.
  *
  * @param urlString Comma-separated list of URLs (relative or absolute)
  * @return List of URLs resolved from {@code urlString}
  */
 protected List<URL> parseURLs(FileObject root, String urlString) {
   if (urlString == null || urlString.trim().isEmpty()) {
     return Collections.emptyList();
   }
   String[] paths = urlString.split(",");
   List<URL> urls = new ArrayList<URL>();
   for (String path : paths) {
     try {
       FileObject file = root.resolveFile(path.trim());
       if (!file.exists()) {
         file = defaultFsm.resolveFile(path.trim());
       }
       if (FileType.FOLDER.equals(file.getType())) {
         // Add directories with a trailing / so the URL ClassLoader interprets
         // them as directories
         urls.add(new URL(file.getURL().toExternalForm() + "/"));
         // Also add all jars within this directory
         urls.addAll(findJarsIn(file, 1, new HashSet<String>()));
       } else {
         urls.add(file.getURL());
       }
     } catch (Exception e) {
       // Log invalid path
       logger.error(BaseMessages.getString(PKG, "Error.InvalidClasspathEntry", path));
     }
   }
   return urls;
 }
コード例 #5
0
ファイル: Shell.java プロジェクト: penSecIT/commons-vfs
  /** Does a 'cp' command. */
  private void cp(final String[] cmd) throws Exception {
    if (cmd.length < 3) {
      throw new Exception("USAGE: cp <src> <dest>");
    }

    final FileObject src = mgr.resolveFile(cwd, cmd[1]);
    FileObject dest = mgr.resolveFile(cwd, cmd[2]);
    if (dest.exists() && dest.getType() == FileType.FOLDER) {
      dest = dest.resolveFile(src.getName().getBaseName());
    }

    dest.copyFrom(src, Selectors.SELECT_ALL);
  }
コード例 #6
0
  public void updateParentFileCombo(FileObject selectedItem) {
    try {
      List<FileObject> parentChain = new ArrayList<FileObject>();
      // are we a directory?
      try {
        if (selectedItem.getType() == FileType.FOLDER && selectedItem.getType().hasChildren()) {
          // we have real children....
          parentChain.add(selectedItem);
        }
      } catch (Exception e) {
        // we are not a folder
      }
      FileObject parentFileObject;
      parentFileObject = selectedItem.getParent();
      while (parentFileObject != null) {
        parentChain.add(parentFileObject);
        parentFileObject = parentFileObject.getParent();
      }

      File roots[] = File.listRoots();
      if (currentPanel != null) {
        for (int i = 0; i < roots.length; i++) {
          parentChain.add(currentPanel.resolveFile(roots[i].getAbsolutePath()));
        }
      }

      String items[] = new String[parentChain.size()];
      int idx = 0;
      for (int i = parentChain.size() - 1; i >= 0; i--) {
        items[idx++] = ((FileObject) parentChain.get(i)).getName().getURI();
      }

      openFileCombo.setItems(items);
      openFileCombo.select(items.length - 1);
    } catch (Exception e) {
      e.printStackTrace();
      // then let's not update the GUI
    }
  }
コード例 #7
0
  @Override
  public void validatePlatform() throws InstallException {

    super.validatePlatform();
    try {

      boolean valid = true;

      FileObject catalinaJar = targetLibDir.resolveFile("catalina.jar");

      if (catalinaJar == null
          || !catalinaJar.exists()
          || !catalinaJar.getType().getName().equals(FileType.FILE.getName())) {
        valid = false;
        getPrinter().printErrStatus("CatalinaHome", "Cannot find catalina");
      }

      FileObject serverXml = targetConfDir.resolveFile("server.xml");
      if (serverXml == null
          || !serverXml.exists()
          || !serverXml.getType().getName().equals(FileType.FILE.getName())) {
        valid = false;
        getPrinter().printErrStatus("CatalinaHome", "Cannot find server.xml");
      }

      // TODO : Validate Version ?

      if (!valid)
        throw new InstallException(
            "Target does not seem a " + getTargetPlatform().getDescription() + " install.");

    } catch (IOException e) {
      getPrinter().printErrStatus("CatalinaHome", e.getMessage());
      throw new InstallException(e.getMessage(), e);
    }

    getPrinter().printOkStatus("CatalinaHome");
  }
コード例 #8
0
ファイル: ZipLister.java プロジェクト: shrekwang/jvifm
  public void enterPath() {
    FileObject fileObject = (FileObject) table.getItem(currentRow).getData("fileObject");

    try {
      if (fileObject.getType().equals(FileType.FOLDER)) {
        historyManager.setSelectedItem(
            currentFileObject.getName().getPath(), fileObject.getName().getBaseName());
        currentFileObject = currentFileObject.getChild(fileObject.getName().getBaseName());
        changeCurrentNode();
        textLocation.setText(currentFileObject.getName().getPath());
      } else {
        editFile(fileObject);
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
コード例 #9
0
ファイル: Shell.java プロジェクト: penSecIT/commons-vfs
 /** Lists the children of a folder. */
 private void listChildren(final FileObject dir, final boolean recursive, final String prefix)
     throws FileSystemException {
   final FileObject[] children = dir.getChildren();
   for (int i = 0; i < children.length; i++) {
     final FileObject child = children[i];
     System.out.print(prefix);
     System.out.print(child.getName().getBaseName());
     if (child.getType() == FileType.FOLDER) {
       System.out.println("/");
       if (recursive) {
         listChildren(child, recursive, prefix + "    ");
       }
     } else {
       System.out.println();
     }
   }
 }
コード例 #10
0
  /**
   * Check existence of a local file
   *
   * @param filename
   * @return true, if file exists
   */
  public boolean FileExists(String filename) {

    FileObject file = null;
    try {
      file = KettleVFS.getFileObject(filename, this);
      if (!file.exists()) {
        return false;
      } else {
        if (file.getType() == FileType.FILE) {
          return true;
        } else {
          return false;
        }
      }
    } catch (Exception e) {
      return false;
    }
  }
コード例 #11
0
    /**
     * Called whenever the value of the selection changes.
     *
     * @param e the event that characterizes the change.
     */
    public void valueChanged(final ListSelectionEvent e) {
      final int selectedRow = getTable().getSelectedRow();
      if (selectedRow == -1) {
        return;
      }

      final FileObject selectedFileObject = getTable().getSelectedFileObject(selectedRow);
      if (selectedFileObject == null) {
        return;
      }

      try {
        if (selectedFileObject.getType() == FileType.FILE) {
          getFileNameTextField().setText(selectedFileObject.getName().getBaseName());
        }
      } catch (FileSystemException e1) {
        // ignore ..
      }
    }
コード例 #12
0
  protected boolean validateInputs(final boolean onConfirm) {
    if (super.validateInputs(onConfirm) == false) {
      return false;
    }

    if (onConfirm == false) {
      return true;
    }

    final String reportName = getFileNameTextField().getText();
    if (StringUtils.isEmpty(reportName) == false
        && reportName.endsWith(REPORT_BUNDLE_EXTENSION) == false) {
      final String safeReportName = reportName + REPORT_BUNDLE_EXTENSION;
      getFileNameTextField().setText(safeReportName);
    }

    try {
      final FileObject selectedView = getSelectedView();
      final String validateName = getSelectedFile();
      if (validateName == null || selectedView == null) {
        return false;
      }

      final FileObject targetFile = selectedView.resolveFile(getFileNameTextField().getText());
      final FileObject fileObject = selectedView.getFileSystem().resolveFile(targetFile.getName());
      if (fileObject.getType() == FileType.IMAGINARY) {
        return true;
      }

      final int result =
          JOptionPane.showConfirmDialog(
              this,
              Messages.getInstance()
                  .formatMessage("PublishToServerAction.FileExistsOverride", validateName),
              Messages.getInstance().getString("PublishToServerAction.Information.Title"),
              JOptionPane.YES_NO_OPTION);
      return result == JOptionPane.YES_OPTION;
    } catch (FileSystemException fse) {
      UncaughtExceptionsModel.getInstance().addException(fse);
      return false;
    }
  }
コード例 #13
0
  @Override
  public boolean backupAgentConfigurations(boolean remove) {
    try {
      super.backupAgentConfigurations(remove);
      // backup jaas.conf
      FileObject jaasConfigFile = targetConfDir.resolveFile("jaas.conf");
      if (jaasConfigFile.exists()) {
        // backup file in the same folder it is installed
        backupFile(jaasConfigFile, jaasConfigFile.getParent());
        if (remove) {
          jaasConfigFile.delete();
        }
      }
      // backup setenv.sh and setenv.bat
      FileObject[] libs = targetBinDir.getChildren();
      for (int i = 0; i < libs.length; i++) {
        FileObject cfgFile = libs[i];

        if (!cfgFile.getType().getName().equals(FileType.FILE.getName())) {
          // ignore folders
          continue;
        }
        if (cfgFile.getName().getBaseName().startsWith("setenv")
            && (cfgFile.getName().getBaseName().endsWith(".sh")
                || cfgFile.getName().getBaseName().endsWith(".bat"))) {
          // backup files in the same folder they're installed in
          backupFile(cfgFile, cfgFile.getParent());
          if (remove) {
            cfgFile.delete();
          }
        }
      }
    } catch (Exception e) {
      getPrinter().printErrStatus("BackupAgentConfigurations", e.getMessage());
      return false;
    }
    return true;
  }
コード例 #14
0
  public void installApplication(JOSSOArtifact artifact, boolean replace) throws InstallException {

    try {

      if (!artifact.getType().equals("war")) {
        log.debug("Application type : " + artifact.getType() + " not supported.");
        return;
      }

      // If the war is already expanded, copy it with a new name.
      FileObject srcFile = getFileSystemManager().resolveFile(artifact.getLocation());

      // Is this the josso gateaway ?
      String name = artifact.getBaseName();
      boolean isFolder = srcFile.getType().equals(FileType.FOLDER);

      if (name.startsWith("josso-gateway-web")) {
        // INSTALL GWY
        // Do we have to explode the war ?
        if (getTargetPlatform().isJOSSOWarExploded() && !isFolder) {
          installJar(srcFile, this.targetDeployDir, "josso", true, replace);
        } else {
          installFile(srcFile, this.targetDeployDir, "josso.war", replace);
        }
        return;
      }

      if (artifact.getBaseName().startsWith("josso-partner-tomcat-web")) {
        installJar(srcFile, this.targetDeployDir, "partnerapp", true, replace);
        return;
      }

    } catch (IOException e) {
      throw new InstallException(e.getMessage(), e);
    }
  }
コード例 #15
0
  public FileObject open(
      Shell applicationShell,
      String[] schemeRestrictions,
      String initialScheme,
      boolean showFileScheme,
      String fileName,
      String[] fileFilters,
      String[] fileFilterNames,
      boolean returnUserAuthenticatedFile,
      int fileDialogMode,
      boolean showLocation,
      boolean showCustomUI) {

    this.fileDialogMode = fileDialogMode;
    this.fileFilters = fileFilters;
    this.fileFilterNames = fileFilterNames;
    this.applicationShell = applicationShell;
    this.showFileScheme = showFileScheme;
    this.initialScheme = initialScheme;
    this.schemeRestrictions = schemeRestrictions;
    this.showLocation = showLocation;
    this.showCustomUI = showCustomUI;

    FileObject tmpInitialFile = initialFile;

    if (defaultInitialFile != null && rootFile == null) {
      try {
        rootFile = defaultInitialFile.getFileSystem().getRoot();
        initialFile = defaultInitialFile;
      } catch (FileSystemException ignored) {
        // well we tried
      }
    }

    createDialog(applicationShell);

    if (!showLocation) {
      comboPanel.setParent(fakeShell);
    } else {
      comboPanel.setParent(customUIPanel);
    }

    if (!showCustomUI) {
      customUIPanel.setParent(fakeShell);
    } else {
      customUIPanel.setParent(dialog);
    }

    // create our file chooser tool bar, contains parent folder combo and various controls
    createToolbarPanel(dialog);
    // create our vfs browser component
    createVfsBrowser(dialog);
    populateCustomUIPanel(dialog);
    if (fileDialogMode == VFS_DIALOG_SAVEAS) {
      createFileNamePanel(dialog, fileName);
    } else {
      // create file filter panel
      createFileFilterPanel(dialog);
    }
    // create our ok/cancel buttons
    createButtonPanel(dialog);

    initialFile = tmpInitialFile;
    // set the initial file selection
    try {
      if (initialFile != null || rootFile != null) {
        vfsBrowser.selectTreeItemByFileObject(initialFile != null ? initialFile : rootFile, true);
        updateParentFileCombo(initialFile != null ? initialFile : rootFile);
        setSelectedFile(initialFile != null ? initialFile : rootFile);
        openFileCombo.setText(
            initialFile != null ? initialFile.getName().getURI() : rootFile.getName().getURI());
      }
    } catch (FileSystemException e) {
      MessageBox box = new MessageBox(dialog.getShell());
      box.setText(Messages.getString("VfsFileChooserDialog.error")); // $NON-NLS-1$
      box.setMessage(e.getMessage());
      box.open();
    }

    // set the size and show the dialog
    int height = 550;
    int width = 800;
    dialog.setSize(width, height);
    Rectangle bounds = dialog.getDisplay().getPrimaryMonitor().getClientArea();
    int x = (bounds.width - width) / 2;
    int y = (bounds.height - height) / 2;
    dialog.setLocation(x, y);
    dialog.open();

    if (rootFile != null && fileDialogMode == VFS_DIALOG_SAVEAS) {
      if (!rootFile.getFileSystem().hasCapability(Capability.WRITE_CONTENT)) {
        MessageBox messageDialog = new MessageBox(dialog.getShell(), SWT.OK);
        messageDialog.setText(Messages.getString("VfsFileChooserDialog.warning")); // $NON-NLS-1$
        messageDialog.setMessage(
            Messages.getString("VfsFileChooserDialog.noWriteSupport")); // $NON-NLS-1$
        messageDialog.open();
      }
    }

    vfsBrowser.fileSystemTree.forceFocus();
    while (!dialog.isDisposed()) {
      if (!dialog.getDisplay().readAndDispatch()) dialog.getDisplay().sleep();
    }

    // we just woke up, we are probably disposed already..
    if (!dialog.isDisposed()) {
      hideCustomPanelChildren();
      dialog.dispose();
    }
    if (okPressed) {
      FileObject returnFile = vfsBrowser.getSelectedFileObject();
      if (returnFile != null && fileDialogMode == VFS_DIALOG_SAVEAS) {
        try {
          if (returnFile.getType().equals(FileType.FILE)) {
            returnFile = returnFile.getParent();
          }
          returnFile = returnFile.resolveFile(enteredFileName);
        } catch (FileSystemException e) {
          e.printStackTrace();
        }
      }

      // put user/pass on the filename so it comes out in the getUri call.
      if (!returnUserAuthenticatedFile) {
        // make sure to put the user/pass on the url if it's not there
        if (returnFile != null && returnFile.getName() instanceof URLFileName) {
          URLFileName urlFileName = (URLFileName) returnFile.getName();
          if (urlFileName.getUserName() == null || urlFileName.getPassword() == null) {
            // set it
            String user = "";
            String pass = "";

            UserAuthenticator userAuthenticator =
                DefaultFileSystemConfigBuilder.getInstance()
                    .getUserAuthenticator(returnFile.getFileSystem().getFileSystemOptions());

            if (userAuthenticator != null) {
              UserAuthenticationData data =
                  userAuthenticator.requestAuthentication(AUTHENTICATOR_TYPES);
              user = String.valueOf(data.getData(UserAuthenticationData.USERNAME));
              pass = String.valueOf(data.getData(UserAuthenticationData.PASSWORD));
              try {
                user = URLEncoder.encode(user, "UTF-8");
                pass = URLEncoder.encode(pass, "UTF-8");
              } catch (UnsupportedEncodingException e) {
                // ignore, just use the un encoded values
              }
            }

            // build up the url with user/pass on it
            int port = urlFileName.getPort();
            String portString = (port < 1) ? "" : (":" + port);
            String urlWithUserPass =
                urlFileName.getScheme()
                    + "://"
                    + user
                    + ":"
                    + pass
                    + "@"
                    + urlFileName.getHostName()
                    + portString
                    + urlFileName.getPath();

            try {
              returnFile = currentPanel.resolveFile(urlWithUserPass);
            } catch (FileSystemException e) {
              // couldn't resolve with user/pass on url??? interesting
              e.printStackTrace();
            }
          }
        }
      }
      return returnFile;
    } else {
      return null;
    }
  }
コード例 #16
0
ファイル: Xslt.java プロジェクト: hzy001/pentaho-kettle
  public boolean processRow(StepMetaInterface smi, StepDataInterface sdi) throws KettleException {
    meta = (XsltMeta) smi;
    data = (XsltData) sdi;

    Object[] row = getRow();

    if (row == null) { // no more input to be expected...
      setOutputDone();
      return false;
    }
    if (first) {
      first = false;
      data.outputRowMeta = getInputRowMeta().clone();
      meta.getFields(data.outputRowMeta, getStepname(), null, null, this, repository, metaStore);

      // Check if The result field is given
      if (Const.isEmpty(meta.getResultfieldname())) {
        // Result Field is missing !
        logError(BaseMessages.getString(PKG, "Xslt.Log.ErrorResultFieldMissing"));
        throw new KettleStepException(
            BaseMessages.getString(PKG, "Xslt.Exception.ErrorResultFieldMissing"));
      }

      // Check if The XML field is given
      if (Const.isEmpty(meta.getFieldname())) {
        // Result Field is missing !
        logError(BaseMessages.getString(PKG, "Xslt.Exception.ErrorXMLFieldMissing"));
        throw new KettleStepException(
            BaseMessages.getString(PKG, "Xslt.Exception.ErrorXMLFieldMissing"));
      }

      // Try to get XML Field index
      data.fieldposition = getInputRowMeta().indexOfValue(meta.getFieldname());
      // Let's check the Field
      if (data.fieldposition < 0) {
        // The field is unreachable !
        logError(
            BaseMessages.getString(PKG, "Xslt.Log.ErrorFindingField")
                + "["
                + meta.getFieldname()
                + "]");
        throw new KettleStepException(
            BaseMessages.getString(PKG, "Xslt.Exception.CouldnotFindField", meta.getFieldname()));
      }

      // Check if the XSL Filename is contained in a column
      if (meta.useXSLField()) {
        if (Const.isEmpty(meta.getXSLFileField())) {
          // The field is missing
          // Result field is missing !
          logError(BaseMessages.getString(PKG, "Xslt.Log.ErrorXSLFileFieldMissing"));
          throw new KettleStepException(
              BaseMessages.getString(PKG, "Xslt.Exception.ErrorXSLFileFieldMissing"));
        }

        // Try to get Field index
        data.fielxslfiledposition = getInputRowMeta().indexOfValue(meta.getXSLFileField());

        // Let's check the Field
        if (data.fielxslfiledposition < 0) {
          // The field is unreachable !
          logError(
              BaseMessages.getString(PKG, "Xslt.Log.ErrorXSLFileFieldFinding")
                  + "["
                  + meta.getXSLFileField()
                  + "]");
          throw new KettleStepException(
              BaseMessages.getString(
                  PKG, "Xslt.Exception.ErrorXSLFileFieldFinding", meta.getXSLFileField()));
        }

      } else {
        if (Const.isEmpty(meta.getXslFilename())) {
          logError(BaseMessages.getString(PKG, "Xslt.Log.ErrorXSLFile"));
          throw new KettleStepException(BaseMessages.getString(PKG, "Xslt.Exception.ErrorXSLFile"));
        }

        // Check if XSL File exists!
        data.xslfilename = environmentSubstitute(meta.getXslFilename());
        FileObject file = null;
        try {
          file = KettleVFS.getFileObject(data.xslfilename);
          if (!file.exists()) {
            logError(
                BaseMessages.getString(PKG, "Xslt.Log.ErrorXSLFileNotExists", data.xslfilename));
            throw new KettleStepException(
                BaseMessages.getString(
                    PKG, "Xslt.Exception.ErrorXSLFileNotExists", data.xslfilename));
          }
          if (file.getType() != FileType.FILE) {
            logError(BaseMessages.getString(PKG, "Xslt.Log.ErrorXSLNotAFile", data.xslfilename));
            throw new KettleStepException(
                BaseMessages.getString(PKG, "Xslt.Exception.ErrorXSLNotAFile", data.xslfilename));
          }
        } catch (Exception e) {
          throw new KettleStepException(e);
        } finally {
          try {
            if (file != null) {
              file.close();
            }
          } catch (Exception e) {
            /* Ignore */
          }
        }
      }

      // Check output parameters
      int nrOutputProps =
          meta.getOutputPropertyName() == null ? 0 : meta.getOutputPropertyName().length;
      if (nrOutputProps > 0) {
        data.outputProperties = new Properties();
        for (int i = 0; i < nrOutputProps; i++) {
          data.outputProperties.put(
              meta.getOutputPropertyName()[i],
              environmentSubstitute(meta.getOutputPropertyValue()[i]));
        }
        data.setOutputProperties = true;
      }

      // Check parameters
      data.nrParams = meta.getParameterField() == null ? 0 : meta.getParameterField().length;
      if (data.nrParams > 0) {
        data.indexOfParams = new int[data.nrParams];
        data.nameOfParams = new String[data.nrParams];
        for (int i = 0; i < data.nrParams; i++) {
          String name = environmentSubstitute(meta.getParameterName()[i]);
          String field = environmentSubstitute(meta.getParameterField()[i]);
          if (Const.isEmpty(field)) {
            throw new KettleStepException(
                BaseMessages.getString(PKG, "Xslt.Exception.ParameterFieldMissing", name, i));
          }
          data.indexOfParams[i] = getInputRowMeta().indexOfValue(field);
          if (data.indexOfParams[i] < 0) {
            throw new KettleStepException(
                BaseMessages.getString(PKG, "Xslt.Exception.ParameterFieldNotFound", name));
          }
          data.nameOfParams[i] = name;
        }
        data.useParameters = true;
      }

      data.factory = TransformerFactory.newInstance();

      if (meta.getXSLFactory().equals("SAXON")) {
        // Set the TransformerFactory to the SAXON implementation.
        data.factory = new net.sf.saxon.TransformerFactoryImpl();
      }
    } // end if first

    // Get the field value
    String xmlValue = getInputRowMeta().getString(row, data.fieldposition);

    if (meta.useXSLField()) {
      // Get the value
      data.xslfilename = getInputRowMeta().getString(row, data.fielxslfiledposition);
      if (log.isDetailed()) {
        logDetailed(
            BaseMessages.getString(
                PKG, "Xslt.Log.XslfileNameFromFied", data.xslfilename, meta.getXSLFileField()));
      }
    }

    try {

      if (log.isDetailed()) {
        if (meta.isXSLFieldIsAFile()) {
          logDetailed(BaseMessages.getString(PKG, "Xslt.Log.Filexsl") + data.xslfilename);
        } else {
          logDetailed(BaseMessages.getString(PKG, "Xslt.Log.XslStream", data.xslfilename));
        }
      }

      // Get the template from the cache
      Transformer transformer = data.getTemplate(data.xslfilename, data.xslIsAfile);

      // Do we need to set output properties?
      if (data.setOutputProperties) {
        transformer.setOutputProperties(data.outputProperties);
      }

      // Do we need to pass parameters?
      if (data.useParameters) {
        for (int i = 0; i < data.nrParams; i++) {
          transformer.setParameter(data.nameOfParams[i], row[data.indexOfParams[i]]);
        }
      }

      Source source = new StreamSource(new StringReader(xmlValue));
      // Prepare output stream
      StreamResult result = new StreamResult(new StringWriter());
      // transform xml source
      transformer.transform(source, result);

      String xmlString = result.getWriter().toString();
      if (log.isDetailed()) {
        logDetailed(BaseMessages.getString(PKG, "Xslt.Log.FileResult"));
        logDetailed(xmlString);
      }
      Object[] outputRowData = RowDataUtil.addValueData(row, getInputRowMeta().size(), xmlString);

      if (log.isRowLevel()) {
        logRowlevel(
            BaseMessages.getString(PKG, "Xslt.Log.ReadRow")
                + " "
                + getInputRowMeta().getString(row));
      }

      // add new values to the row.
      putRow(data.outputRowMeta, outputRowData); // copy row to output rowset(s);

    } catch (Exception e) {
      String errorMessage = e.getMessage();
      StringWriter sw = new StringWriter();
      PrintWriter pw = new PrintWriter(sw);
      DefaultErrorHandler.printLocation(pw, e);
      pw.close();
      errorMessage = sw.toString() + "\n" + errorMessage;

      if (getStepMeta().isDoingErrorHandling()) {
        // Simply add this row to the error row
        putError(getInputRowMeta(), row, 1, errorMessage, meta.getResultfieldname(), "XSLT01");
      } else {
        logError(BaseMessages.getString(PKG, "Xslt.ErrorProcesing" + " : " + errorMessage));
        throw new KettleStepException(BaseMessages.getString(PKG, "Xslt.ErrorProcesing"), e);
      }
    }

    return true;
  }
コード例 #17
0
  public Result execute(Result previousResult, int nr) {
    Result result = previousResult;
    result.setResult(false);

    // starttime (in seconds)
    long timeStart = System.currentTimeMillis() / 1000;

    if (filename != null) {
      FileObject fileObject = null;
      String realFilename = getRealFilename();
      try {
        fileObject = KettleVFS.getFileObject(realFilename, this);

        long iMaximumTimeout =
            Const.toInt(getRealMaximumTimeout(), Const.toInt(DEFAULT_MAXIMUM_TIMEOUT, 0));
        long iCycleTime =
            Const.toInt(getRealCheckCycleTime(), Const.toInt(DEFAULT_CHECK_CYCLE_TIME, 0));

        //
        // Sanity check on some values, and complain on insanity
        //
        if (iMaximumTimeout < 0) {
          iMaximumTimeout = Const.toInt(DEFAULT_MAXIMUM_TIMEOUT, 0);
          if (log.isBasic()) logBasic("Maximum timeout invalid, reset to " + iMaximumTimeout);
        }

        if (iCycleTime < 1) {
          // If lower than 1 set to the default
          iCycleTime = Const.toInt(DEFAULT_CHECK_CYCLE_TIME, 1);
          if (log.isBasic()) logBasic("Check cycle time invalid, reset to " + iCycleTime);
        }

        if (iMaximumTimeout == 0) {
          if (log.isBasic()) logBasic("Waiting indefinitely for file [" + realFilename + "]");
        } else {
          if (log.isBasic())
            logBasic("Waiting " + iMaximumTimeout + " seconds for file [" + realFilename + "]");
        }

        boolean continueLoop = true;
        while (continueLoop && !parentJob.isStopped()) {
          fileObject = KettleVFS.getFileObject(realFilename, this);

          if (fileObject.exists()) {
            // file exists, we're happy to exit
            if (log.isBasic()) logBasic("Detected file [" + realFilename + "] within timeout");
            result.setResult(true);
            continueLoop = false;

            // add filename to result filenames
            if (addFilenameToResult && fileObject.getType() == FileType.FILE) {
              ResultFile resultFile =
                  new ResultFile(
                      ResultFile.FILE_TYPE_GENERAL, fileObject, parentJob.getJobname(), toString());
              resultFile.setComment(BaseMessages.getString(PKG, "JobWaitForFile.FilenameAdded"));
              result.getResultFiles().put(resultFile.getFile().toString(), resultFile);
            }
          } else {
            long now = System.currentTimeMillis() / 1000;

            if ((iMaximumTimeout > 0) && (now > (timeStart + iMaximumTimeout))) {
              continueLoop = false;

              // file doesn't exist after timeout, either true or false
              if (isSuccessOnTimeout()) {
                if (log.isBasic())
                  logBasic("Didn't detect file [" + realFilename + "] before timeout, success");
                result.setResult(true);
              } else {
                if (log.isBasic())
                  logBasic("Didn't detect file [" + realFilename + "] before timeout, failure");
                result.setResult(false);
              }
            }

            // sleep algorithm
            long sleepTime = 0;

            if (iMaximumTimeout == 0) {
              sleepTime = iCycleTime;
            } else {
              if ((now + iCycleTime) < (timeStart + iMaximumTimeout)) {
                sleepTime = iCycleTime;
              } else {
                sleepTime = iCycleTime - ((now + iCycleTime) - (timeStart + iMaximumTimeout));
              }
            }

            try {
              if (sleepTime > 0) {
                if (log.isDetailed()) {
                  logDetailed(
                      "Sleeping "
                          + sleepTime
                          + " seconds before next check for file ["
                          + realFilename
                          + "]");
                }
                Thread.sleep(sleepTime * 1000);
              }
            } catch (InterruptedException e) {
              // something strange happened
              result.setResult(false);
              continueLoop = false;
            }
          }
        }

        if (!parentJob.isStopped() && fileObject.exists() && isFileSizeCheck()) {
          long oldSize = -1;
          long newSize = fileObject.getContent().getSize();

          if (log.isDetailed())
            logDetailed("File [" + realFilename + "] is " + newSize + " bytes long");
          if (log.isBasic())
            logBasic(
                "Waiting until file ["
                    + realFilename
                    + "] stops growing for "
                    + iCycleTime
                    + " seconds");
          while (oldSize != newSize && !parentJob.isStopped()) {
            try {
              if (log.isDetailed()) {
                logDetailed(
                    "Sleeping "
                        + iCycleTime
                        + " seconds, waiting for file ["
                        + realFilename
                        + "] to stop growing");
              }
              Thread.sleep(iCycleTime * 1000);
            } catch (InterruptedException e) {
              // something strange happened
              result.setResult(false);
              continueLoop = false;
            }
            oldSize = newSize;
            newSize = fileObject.getContent().getSize();
            if (log.isDetailed()) {
              logDetailed("File [" + realFilename + "] is " + newSize + " bytes long");
            }
          }
          if (log.isBasic())
            logBasic("Stopped waiting for file [" + realFilename + "] to stop growing");
        }

        if (parentJob.isStopped()) {
          result.setResult(false);
        }
      } catch (Exception e) {
        logBasic("Exception while waiting for file [" + realFilename + "] to stop growing", e);
      } finally {
        if (fileObject != null) {
          try {
            fileObject.close();
          } catch (Exception e) {
          }
        }
      }
    } else {
      logError("No filename is defined.");
    }

    return result;
  }
コード例 #18
0
ファイル: FileInfoCommand.java プロジェクト: runepeter/gshell
  public Object execute(final CommandContext context) throws Exception {
    assert context != null;
    IO io = context.getIo();

    FileObject file = resolveFile(context, path);

    io.println("URL: {}", file.getURL());
    io.println("Name: {}", file.getName());
    io.println("BaseName: {}", file.getName().getBaseName());
    io.println("Extension: {}", file.getName().getExtension());
    io.println("Path: {}", file.getName().getPath());
    io.println("Scheme: {}", file.getName().getScheme());
    io.println("URI: {}", file.getName().getURI());
    io.println("Root URI: {}", file.getName().getRootURI());
    io.println("Parent: {}", file.getName().getParent());
    io.println("Type: {}", file.getType());
    io.println("Exists: {}", file.exists());
    io.println("Readable: {}", file.isReadable());
    io.println("Writeable: {}", file.isWriteable());
    io.println("Root path: {}", file.getFileSystem().getRoot().getName().getPath());

    if (file.exists()) {
      FileContent content = file.getContent();
      FileContentInfo contentInfo = content.getContentInfo();
      io.println("Content type: {}", contentInfo.getContentType());
      io.println("Content encoding: {}", contentInfo.getContentEncoding());

      try {
        // noinspection unchecked
        Map<String, Object> attrs = content.getAttributes();
        if (attrs != null && !attrs.isEmpty()) {
          io.println("Attributes:");
          for (Map.Entry<String, Object> entry : attrs.entrySet()) {
            io.println("    {}='{}'", entry.getKey(), entry.getValue());
          }
        }
      } catch (FileSystemException e) {
        io.println("File attributes are NOT supported");
      }

      try {
        Certificate[] certs = content.getCertificates();
        if (certs != null && certs.length != 0) {
          io.println("Certificate:");
          for (Certificate cert : certs) {
            io.println("    {}", cert);
          }
        }
      } catch (FileSystemException e) {
        io.println("File certificates are NOT supported");
      }

      if (file.getType().equals(FileType.FILE)) {
        io.println("Size: {} bytes", content.getSize());
      } else if (file.getType().hasChildren() && file.isReadable()) {
        FileObject[] children = file.getChildren();
        io.println("Directory with {} files", children.length);

        for (int iterChildren = 0; iterChildren < children.length; iterChildren++) {
          io.println("#{}:{}", iterChildren, children[iterChildren].getName());
          if (iterChildren > 5) {
            break;
          }
        }
      }

      io.println(
          "Last modified: {}",
          DateFormat.getInstance().format(new Date(content.getLastModifiedTime())));
    } else {
      io.println("The file does not exist");
    }

    FileObjects.close(file);

    return Result.SUCCESS;
  }