text:
package org.apache.twill.yarn;

import com.google.common.base.Function;
import com.google.common.base.Joiner;
import com.google.common.base.Preconditions;
import com.google.common.base.Throwables;
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.HashMultimap;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterables;
import com.google.common.collect.ListMultimap;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Multimap;
import com.google.common.collect.Sets;
import com.google.common.hash.Hasher;
import com.google.common.hash.Hashing;
import com.google.common.io.ByteStreams;
import com.google.common.io.OutputSupplier;
import com.google.common.reflect.TypeToken;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import joptsimple.OptionSpec;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.security.Credentials;
import org.apache.hadoop.security.UserGroupInformation;
import org.apache.hadoop.security.token.Token;
import org.apache.hadoop.yarn.conf.YarnConfiguration;
import org.apache.twill.api.ClassAcceptor;
import org.apache.twill.api.Configs;
import org.apache.twill.api.EventHandlerSpecification;
import org.apache.twill.api.LocalFile;
import org.apache.twill.api.RunId;
import org.apache.twill.api.RuntimeSpecification;
import org.apache.twill.api.SecureStore;
import org.apache.twill.api.TwillController;
import org.apache.twill.api.TwillPreparer;
import org.apache.twill.api.TwillSpecification;
import org.apache.twill.api.logging.LogEntry;
import org.apache.twill.api.logging.LogHandler;
import org.apache.twill.filesystem.Location;
import org.apache.twill.internal.ApplicationBundler;
import org.apache.twill.internal.Arguments;
import org.apache.twill.internal.Constants;
import org.apache.twill.internal.DefaultLocalFile;
import org.apache.twill.internal.DefaultRuntimeSpecification;
import org.apache.twill.internal.DefaultTwillSpecification;
import org.apache.twill.internal.EnvKeys;
import org.apache.twill.internal.JvmOptions;
import org.apache.twill.internal.LogOnlyEventHandler;
import org.apache.twill.internal.ProcessController;
import org.apache.twill.internal.ProcessLauncher;
import org.apache.twill.internal.TwillRuntimeSpecification;
import org.apache.twill.internal.appmaster.ApplicationMasterInfo;
import org.apache.twill.internal.appmaster.ApplicationMasterMain;
import org.apache.twill.internal.container.TwillContainerMain;
import org.apache.twill.internal.io.LocationCache;
import org.apache.twill.internal.json.ArgumentsCodec;
import org.apache.twill.internal.json.JvmOptionsCodec;
import org.apache.twill.internal.json.LocalFileCodec;
import org.apache.twill.internal.json.TwillRuntimeSpecificationAdapter;
import org.apache.twill.internal.utils.Dependencies;
import org.apache.twill.internal.utils.Paths;
import org.apache.twill.internal.utils.Resources;
import org.apache.twill.internal.yarn.VersionDetectYarnAppClientFactory;
import org.apache.twill.internal.yarn.YarnAppClient;
import org.apache.twill.internal.yarn.YarnApplicationReport;
import org.apache.twill.internal.yarn.YarnUtils;
import org.apache.twill.launcher.FindFreePort;
import org.apache.twill.launcher.TwillLauncher;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.Callable;
import java.util.concurrent.TimeUnit;
import java.util.jar.JarEntry;
import java.util.jar.JarOutputStream;

/**
 * Implementation for {@link TwillPreparer} to prepare and launch distributed application on Hadoop YARN.
 */
final class YarnTwillPreparer implements TwillPreparer {

  private static final Logger LOG = LoggerFactory.getLogger(YarnTwillPreparer.class);
  private static final Function<Class<?>, String> CLASS_TO_NAME = new Function<Class<?>, String>() {
    @Override
    public String apply(Class<?> cls) {
      return cls.getName();
    }
  };

  private final Configuration config;
  private final TwillSpecification twillSpec;
  private final String zkConnectString;
  private final Location appLocation;
  private final YarnTwillControllerFactory controllerFactory;
  private final RunId runId;

  private final List<LogHandler> logHandlers = Lists.newArrayList();
  private final List<String> arguments = Lists.newArrayList();
  private final Set<Class<?>> dependencies = Sets.newIdentityHashSet();
  private final List<URI> resources = Lists.newArrayList();
  private final List<String> classPaths = Lists.newArrayList();
  private final ListMultimap<String, String> runnableArgs = ArrayListMultimap.create();
  private final Map<String, Map<String, String>> environments = Maps.newHashMap();
  private final List<String> applicationClassPaths = Lists.newArrayList();
  private final Credentials credentials;
  private final Map<String, Map<String, String>> logLevels = Maps.newHashMap();
  private final LocationCache locationCache;
  private String schedulerQueue;
  private String extraOptions;
  private JvmOptions.DebugOptions debugOptions = JvmOptions.DebugOptions.NO_DEBUG;
  private ClassAcceptor classAcceptor;
  private final Map<String, Integer> maxRetries = Maps.newHashMap();

  YarnTwillPreparer(Configuration config, TwillSpecification twillSpec, RunId runId,
                    String zkConnectString, Location appLocation, String extraOptions,
                    LocationCache locationCache, YarnTwillControllerFactory controllerFactory) {
    this.config = config;
    this.twillSpec = twillSpec;
    this.runId = runId;
    this.zkConnectString = zkConnectString;
    this.appLocation = appLocation;
    this.controllerFactory = controllerFactory;
    this.credentials = createCredentials();
    this.extraOptions = extraOptions;
    this.classAcceptor = new ClassAcceptor();
    this.locationCache = locationCache;
  }

  private void confirmRunnableName(String runnableName) {
    Preconditions.checkNotNull(runnableName);
    Preconditions.checkArgument(twillSpec.getRunnables().containsKey(runnableName),
                                "Runnable %s is not defined in the application.", runnableName);
  }

  @Override
  public TwillPreparer withConfiguration(Map<String, String> config) {
    for (Map.Entry<String, String> entry : config.entrySet()) {
      this.config.set(entry.getKey(), entry.getValue());
    }
    return this;
  }

  @Override
  public TwillPreparer addLogHandler(LogHandler handler) {
    logHandlers.add(handler);
    return this;
  }

  @Override
  public TwillPreparer setUser(String user) {
    return this;
  }

  @Override
  public TwillPreparer setSchedulerQueue(String name) {
    this.schedulerQueue = name;
    return this;
  }

  @Override
  public TwillPreparer setJVMOptions(String options) {
    this.extraOptions = options;
    return this;
  }

  @Override
  public TwillPreparer addJVMOptions(String options) {
    this.extraOptions = extraOptions == null ? options : extraOptions + " " + options;
    return this;
  }

  @Override
  public TwillPreparer enableDebugging(String... runnables) {
    return enableDebugging(false, runnables);
  }

  @Override
  public TwillPreparer enableDebugging(boolean doSuspend, String... runnables) {
    this.debugOptions = new JvmOptions.DebugOptions(true, doSuspend, ImmutableSet.copyOf(runnables));
    return this;
  }

  @Override
  public TwillPreparer withApplicationArguments(String... args) {
    return withApplicationArguments(ImmutableList.copyOf(args));
  }

  @Override
  public TwillPreparer withApplicationArguments(Iterable<String> args) {
    Iterables.addAll(arguments, args);
    return this;
  }

  @Override
  public TwillPreparer withArguments(String runnableName, String... args) {
    return withArguments(runnableName, ImmutableList.copyOf(args));
  }

  @Override
  public TwillPreparer withArguments(String runnableName, Iterable<String> args) {
    confirmRunnableName(runnableName);
    runnableArgs.putAll(runnableName, args);
    return this;
  }

  @Override
  public TwillPreparer withDependencies(Class<?>... classes) {
    return withDependencies(ImmutableList.copyOf(classes));
  }

  @Override
  public TwillPreparer withDependencies(Iterable<Class<?>> classes) {
    Iterables.addAll(dependencies, classes);
    return this;
  }

  @Override
  public TwillPreparer withResources(URI... resources) {
    return withResources(ImmutableList.copyOf(resources));
  }

  @Override
  public TwillPreparer withResources(Iterable<URI> resources) {
    Iterables.addAll(this.resources, resources);
    return this;
  }

  @Override
  public TwillPreparer withClassPaths(String... classPaths) {
    return withClassPaths(ImmutableList.copyOf(classPaths));
  }

  @Override
  public TwillPreparer withClassPaths(Iterable<String> classPaths) {
    Iterables.addAll(this.classPaths, classPaths);
    return this;
  }

  @Override
  public TwillPreparer withEnv(Map<String, String> env) {
    // Add the given environments to all runnables
    for (String runnableName : twillSpec.getRunnables().keySet()) {
      setEnv(runnableName, env, false);
    }
    return this;
  }

  @Override
  public TwillPreparer withEnv(String runnableName, Map<String, String> env) {
    confirmRunnableName(runnableName);
    setEnv(runnableName, env, true);
    return this;
  }

  @Override
  public TwillPreparer withApplicationClassPaths(String... classPaths) {
    return withApplicationClassPaths(ImmutableList.copyOf(classPaths));
  }

  @Override
  public TwillPreparer withApplicationClassPaths(Iterable<String> classPaths) {
    Iterables.addAll(this.applicationClassPaths, classPaths);
    return this;
  }

  @Override
  public TwillPreparer withBundlerClassAcceptor(ClassAcceptor classAcceptor) {
    this.classAcceptor = classAcceptor;
    return this;
  }

  @Override
  public TwillPreparer withMaxRetries(String runnableName, int maxRetries) {
    confirmRunnableName(runnableName);
    this.maxRetries.put(runnableName, maxRetries);
    return this;
  }

  @Override
  public TwillPreparer addSecureStore(SecureStore secureStore) {
    Object store = secureStore.getStore();
    Preconditions.checkArgument(store instanceof Credentials, "Only Hadoop Credentials is supported.");
    this.credentials.mergeAll((Credentials) store);
    return this;
  }

  @Override
  public TwillPreparer setLogLevel(LogEntry.Level logLevel) {
    return setLogLevels(ImmutableMap.of(Logger.ROOT_LOGGER_NAME, logLevel));
  }

  @Override
  public TwillPreparer setLogLevels(Map<String, LogEntry.Level> logLevels) {
    Preconditions.checkNotNull(logLevels);
    for (String runnableName : twillSpec.getRunnables().keySet()) {
      saveLogLevels(runnableName, logLevels);
    }
    return this;
  }

  @Override
  public TwillPreparer setLogLevels(String runnableName, Map<String, LogEntry.Level> runnableLogLevels) {
    confirmRunnableName(runnableName);
    Preconditions.checkNotNull(runnableLogLevels);
    Preconditions.checkArgument(!(logLevels.containsKey(Logger.ROOT_LOGGER_NAME)
      && logLevels.get(Logger.ROOT_LOGGER_NAME) == null));
    saveLogLevels(runnableName, runnableLogLevels);
    return this;
  }

  @Override
  public TwillController start() {
    return start(Constants.APPLICATION_MAX_START_SECONDS, TimeUnit.SECONDS);
  }

  @Override
  public TwillController start(long timeout, TimeUnit timeoutUnit) {
    try {
      final YarnAppClient yarnAppClient = new VersionDetectYarnAppClientFactory().create(config);
      final ProcessLauncher<ApplicationMasterInfo> launcher = yarnAppClient.createLauncher(twillSpec, schedulerQueue);
      final ApplicationMasterInfo appMasterInfo = launcher.getContainerInfo();
      Callable<ProcessController<YarnApplicationReport>> submitTask =
        new Callable<ProcessController<YarnApplicationReport>>() {
          @Override
          public ProcessController<YarnApplicationReport> call() throws Exception {

            // Local files needed by AM
            Map<String, LocalFile> localFiles = Maps.newHashMap();

            createLauncherJar(localFiles);
            createTwillJar(createBundler(classAcceptor), yarnAppClient, localFiles);
            createApplicationJar(createBundler(classAcceptor), localFiles);
            createResourcesJar(createBundler(classAcceptor), localFiles);

            Path runtimeConfigDir = Files.createTempDirectory(getLocalStagingDir().toPath(),
                                                              Constants.Files.RUNTIME_CONFIG_JAR);
            try {
              saveSpecification(twillSpec, runtimeConfigDir.resolve(Constants.Files.TWILL_SPEC));
              saveLogback(runtimeConfigDir.resolve(Constants.Files.LOGBACK_TEMPLATE));
              saveClassPaths(runtimeConfigDir);
              saveJvmOptions(runtimeConfigDir.resolve(Constants.Files.JVM_OPTIONS));
              saveArguments(new Arguments(arguments, runnableArgs),
                            runtimeConfigDir.resolve(Constants.Files.ARGUMENTS));
              saveEnvironments(runtimeConfigDir.resolve(Constants.Files.ENVIRONMENTS));
              createRuntimeConfigJar(runtimeConfigDir, localFiles);
            } finally {
              Paths.deleteRecursively(runtimeConfigDir);
            }

            createLocalizeFilesJson(localFiles);

            LOG.debug("Submit AM container spec: {}", appMasterInfo);
            // java -Djava.io.tmpdir=tmp -cp launcher.jar:$HADOOP_CONF_DIR -XmxMemory
            //     org.apache.twill.internal.TwillLauncher
            //     appMaster.jar
            //     org.apache.twill.internal.appmaster.ApplicationMasterMain
            //     false

            int reservedMemoryMB = config.getInt(Configs.Keys.YARN_AM_RESERVED_MEMORY_MB,
                                                 Configs.Defaults.YARN_AM_RESERVED_MEMORY_MB);
            int memory = Resources.computeMaxHeapSize(appMasterInfo.getMemoryMB(), reservedMemoryMB, getMinHeapRatio());
            return launcher.prepareLaunch(ImmutableMap.<String, String>of(), localFiles.values(),
                                          createSubmissionCredentials())
              .addCommand(
                "$JAVA_HOME/bin/java",
                "-Djava.io.tmpdir=tmp",
                "-Dyarn.appId=$" + EnvKeys.YARN_APP_ID_STR,
                "-Dtwill.app=$" + Constants.TWILL_APP_NAME,
                "-cp", Constants.Files.LAUNCHER_JAR + ":$HADOOP_CONF_DIR",
                "-Xmx" + memory + "m",
                extraOptions == null ? "" : extraOptions,
                TwillLauncher.class.getName(),
                ApplicationMasterMain.class.getName(),
                Boolean.FALSE.toString())
              .launch();
          }
        };

      boolean logCollectionEnabled = config.getBoolean(Configs.Keys.LOG_COLLECTION_ENABLED,
                                                       Configs.Defaults.LOG_COLLECTION_ENABLED);
      YarnTwillController controller = controllerFactory.create(runId, logCollectionEnabled,
                                                                logHandlers, submitTask, timeout, timeoutUnit);
      controller.start();
      return controller;
    } catch (Exception e) {
      LOG.error("Failed to submit application {}", twillSpec.getName(), e);
      throw Throwables.propagate(e);
    }
  }

  /**
   * Returns the minimum heap ratio based on the configuration.
   */
  private double getMinHeapRatio() {
    // doing this way to support hadoop-2.0 profile
    String minHeapRatioStr = config.get(Configs.Keys.HEAP_RESERVED_MIN_RATIO);
    return (minHeapRatioStr == null) ? Configs.Defaults.HEAP_RESERVED_MIN_RATIO : Double.parseDouble(minHeapRatioStr);
  }

  /**
   * Returns the reserved memory size in MB based on the configuration.
   */
  private int getReservedMemory() {
    return config.getInt(Configs.Keys.JAVA_RESERVED_MEMORY_MB, Configs.Defaults.JAVA_RESERVED_MEMORY_MB);
  }

  /**
   * Returns the local staging directory based on the configuration.
   */
  private File getLocalStagingDir() {
    return new File(config.get(Configs.Keys.LOCAL_STAGING_DIRECTORY, Configs.Defaults.LOCAL_STAGING_DIRECTORY));
  }

  private void setEnv(String runnableName, Map<String, String> env, boolean overwrite) {
    Map<String, String> environment = environments.get(runnableName);
    if (environment == null) {
      environment = new LinkedHashMap<>(env);
      environments.put(runnableName, environment);
      return;
    }

    for (Map.Entry<String, String> entry : env.entrySet()) {
      if (overwrite || !environment.containsKey(entry.getKey())) {
        environment.put(entry.getKey(), entry.getValue());
      }
    }
  }

  private void saveLogLevels(String runnableName, Map<String, LogEntry.Level> logLevels) {
    Map<String, String> newLevels = new HashMap<>();
    for (Map.Entry<String, LogEntry.Level> entry : logLevels.entrySet()) {
      Preconditions.checkArgument(entry.getValue() != null, "Log level cannot be null for logger {}", entry.getKey());
      newLevels.put(entry.getKey(), entry.getValue().name());
    }
    this.logLevels.put(runnableName, newLevels);
  }

  /**
   * Creates an {@link Credentials} by copying the {@link Credentials} of the current user.
   */
  private Credentials createCredentials() {
    Credentials credentials = new Credentials();

    try {
      credentials.addAll(UserGroupInformation.getCurrentUser().getCredentials());
    } catch (IOException e) {
      LOG.warn("Failed to get current user UGI. Current user credentials not added.", e);
    }
    return credentials;
  }

  /**
   * Creates a {@link Credentials} for the application submission.
   */
  private Credentials createSubmissionCredentials() {
    Credentials credentials = new Credentials();
    try {
      // Acquires delegation token for the location
      List<Token<?>> tokens = YarnUtils.addDelegationTokens(config, appLocation.getLocationFactory(), credentials);
      if (LOG.isDebugEnabled()) {
        for (Token<?> token : tokens) {
          LOG.debug("Delegation token acquired for {}, {}", appLocation, token);
        }
      }
    } catch (IOException e) {
      LOG.warn("Failed to acquire delegation token for location {}", appLocation);
    }

    // Copy the user provided credentials.
    // It will override the location delegation tokens acquired above if user supplies it.
    credentials.addAll(this.credentials);
    return credentials;
  }

  private LocalFile createLocalFile(String name, Location location) throws IOException {
    return createLocalFile(name, location, false);
  }

  private LocalFile createLocalFile(String name, Location location, boolean archive) throws IOException {
    return new DefaultLocalFile(name, location.toURI(), location.lastModified(), location.length(), archive, null);
  }

  private void createTwillJar(final ApplicationBundler bundler,
                              final YarnAppClient yarnAppClient,
                              Map<String, LocalFile> localFiles) throws IOException {
    LOG.debug("Create and copy {}", Constants.Files.TWILL_JAR);
    Location location = locationCache.get(Constants.Files.TWILL_JAR, new LocationCache.Loader() {
      @Override
      public void load(String name, Location targetLocation) throws IOException {
        // Stuck in the yarnAppClient class to make bundler being able to pickup the right yarn-client version
        bundler.createBundle(targetLocation, ApplicationMasterMain.class,
                             yarnAppClient.getClass(), TwillContainerMain.class, OptionSpec.class);
      }
    });

    LOG.debug("Done {}", Constants.Files.TWILL_JAR);
    localFiles.put(Constants.Files.TWILL_JAR, createLocalFile(Constants.Files.TWILL_JAR, location, true));
  }

  private void createApplicationJar(final ApplicationBundler bundler,
                                    Map<String, LocalFile> localFiles) throws IOException {
    try {
      final Set<Class<?>> classes = Sets.newIdentityHashSet();
      classes.addAll(dependencies);

      ClassLoader classLoader = getClassLoader();
      for (RuntimeSpecification spec : twillSpec.getRunnables().values()) {
        classes.add(classLoader.loadClass(spec.getRunnableSpecification().getClassName()));
      }

      // Add the TwillRunnableEventHandler class
      if (twillSpec.getEventHandler() != null) {
        classes.add(getClassLoader().loadClass(twillSpec.getEventHandler().getClassName()));
      }

      // The location name is computed from the MD5 of all the classes names
      // The localized name is always APPLICATION_JAR
      List<String> classList = Lists.newArrayList(Iterables.transform(classes, CLASS_TO_NAME));
      Collections.sort(classList);
      Hasher hasher = Hashing.md5().newHasher();
      for (String name : classList) {
        hasher.putString(name);
      }
      // Only depends on class list so that it can be reused across different launches
      String name = hasher.hash().toString() + "-" + Constants.Files.APPLICATION_JAR;

      LOG.debug("Create and copy {}", Constants.Files.APPLICATION_JAR);
      Location location = locationCache.get(name, new LocationCache.Loader() {
        @Override
        public void load(String name, Location targetLocation) throws IOException {
          bundler.createBundle(targetLocation, classes);
        }
      });

      LOG.debug("Done {}", Constants.Files.APPLICATION_JAR);

      localFiles.put(Constants.Files.APPLICATION_JAR, createLocalFile(Constants.Files.APPLICATION_JAR, location, true));

    } catch (ClassNotFoundException e) {
      throw Throwables.propagate(e);
    }
  }

  private void createResourcesJar(ApplicationBundler bundler, Map<String, LocalFile> localFiles) throws IOException {
    // If there is no resources, no need to create the jar file.
    if (resources.isEmpty()) {
      return;
    }

    LOG.debug("Create and copy {}", Constants.Files.RESOURCES_JAR);
    Location location = createTempLocation(Constants.Files.RESOURCES_JAR);
    bundler.createBundle(location, Collections.<Class<?>>emptyList(), resources);
    LOG.debug("Done {}", Constants.Files.RESOURCES_JAR);
    localFiles.put(Constants.Files.RESOURCES_JAR, createLocalFile(Constants.Files.RESOURCES_JAR, location, true));
  }

  private void createRuntimeConfigJar(Path dir, Map<String, LocalFile> localFiles) throws IOException {
    LOG.debug("Create and copy {}", Constants.Files.RUNTIME_CONFIG_JAR);

    // Jar everything under the given directory, which contains different files needed by AM/runnable containers
    Location location = createTempLocation(Constants.Files.RUNTIME_CONFIG_JAR);
    try (
      JarOutputStream jarOutput = new JarOutputStream(location.getOutputStream());
      DirectoryStream<Path> stream = Files.newDirectoryStream(dir)
    ) {
      for (Path path : stream) {
        jarOutput.putNextEntry(new JarEntry(path.getFileName().toString()));
        Files.copy(path, jarOutput);
        jarOutput.closeEntry();
      }
    }

    LOG.debug("Done {}", Constants.Files.RUNTIME_CONFIG_JAR);
    localFiles.put(Constants.Files.RUNTIME_CONFIG_JAR,
                   createLocalFile(Constants.Files.RUNTIME_CONFIG_JAR, location, true));
  }

  /**
   * Based on the given {@link TwillSpecification}, upload LocalFiles to Yarn Cluster.
   * @param twillSpec The {@link TwillSpecification} for populating resource.
   */
  private Multimap<String, LocalFile> populateRunnableLocalFiles(TwillSpecification twillSpec) throws IOException {
    Multimap<String, LocalFile> localFiles = HashMultimap.create();

    LOG.debug("Populating Runnable LocalFiles");
    for (Map.Entry<String, RuntimeSpecification> entry: twillSpec.getRunnables().entrySet()) {
      String runnableName = entry.getKey();
      for (LocalFile localFile : entry.getValue().getLocalFiles()) {
        Location location;

        URI uri = localFile.getURI();
        if (appLocation.toURI().getScheme().equals(uri.getScheme())) {
          // If the source file location is having the same scheme as the target location, no need to copy
          location = appLocation.getLocationFactory().create(uri);
        } else {
          URL url = uri.toURL();
          LOG.debug("Create and copy {} : {}", runnableName, url);
          // Preserves original suffix for expansion.
          location = copyFromURL(url, createTempLocation(Paths.addExtension(url.getFile(), localFile.getName())));
          LOG.debug("Done {} : {}", runnableName, url);
        }

        localFiles.put(runnableName,
                       new DefaultLocalFile(localFile.getName(), location.toURI(), location.lastModified(),
                                            location.length(), localFile.isArchive(), localFile.getPattern()));
      }
    }
    LOG.debug("Done Runnable LocalFiles");
    return localFiles;
  }

  private void saveSpecification(TwillSpecification spec, Path targetFile) throws IOException {
    final Multimap<String, LocalFile> runnableLocalFiles = populateRunnableLocalFiles(spec);

    // Rewrite LocalFiles inside twillSpec
    Map<String, RuntimeSpecification> runtimeSpec = Maps.transformEntries(
      spec.getRunnables(), new Maps.EntryTransformer<String, RuntimeSpecification, RuntimeSpecification>() {
        @Override
        public RuntimeSpecification transformEntry(String key, RuntimeSpecification value) {
          return new DefaultRuntimeSpecification(value.getName(), value.getRunnableSpecification(),
                                                 value.getResourceSpecification(), runnableLocalFiles.get(key));
        }
      });

    // Serialize into a local temp file.
    LOG.debug("Creating {}", targetFile);
    try (Writer writer = Files.newBufferedWriter(targetFile, StandardCharsets.UTF_8)) {
      EventHandlerSpecification eventHandler = spec.getEventHandler();
      if (eventHandler == null) {
        eventHandler = new LogOnlyEventHandler().configure();
      }
      TwillSpecification newTwillSpec = new DefaultTwillSpecification(spec.getName(), runtimeSpec, spec.getOrders(),
                                                                      spec.getPlacementPolicies(), eventHandler);
      boolean logCollectionEnabled = config.getBoolean(Configs.Keys.LOG_COLLECTION_ENABLED,
                                                       Configs.Defaults.LOG_COLLECTION_ENABLED);
      TwillRuntimeSpecificationAdapter.create().toJson(
        new TwillRuntimeSpecification(newTwillSpec, appLocation.getLocationFactory().getHomeLocation().getName(),
                                      appLocation.toURI(), zkConnectString, runId, twillSpec.getName(),
                                      getReservedMemory(), config.get(YarnConfiguration.RM_SCHEDULER_ADDRESS),
                                      logLevels, maxRetries, getMinHeapRatio(), logCollectionEnabled), writer);
    }
    LOG.debug("Done {}", targetFile);
  }

  private void saveLogback(Path targetFile) throws IOException {
    URL url = getClass().getClassLoader().getResource(Constants.Files.LOGBACK_TEMPLATE);
    if (url == null) {
      return;
    }

    LOG.debug("Creating {}", targetFile);
    try (InputStream is = url.openStream()) {
      Files.copy(is, targetFile);
    }
    LOG.debug("Done {}", targetFile);
  }

  /**
   * Creates the launcher.jar for launch the main application.
   */
  private void createLauncherJar(Map<String, LocalFile> localFiles) throws URISyntaxException, IOException {

    LOG.debug("Create and copy {}", Constants.Files.LAUNCHER_JAR);

    Location location = locationCache.get(Constants.Files.LAUNCHER_JAR, new LocationCache.Loader() {
      @Override
      public void load(String name, Location targetLocation) throws IOException {
        // Create a jar file with the TwillLauncher and FindFreePort and dependent classes inside.
        try (JarOutputStream jarOut = new JarOutputStream(targetLocation.getOutputStream())) {
          ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
          if (classLoader == null) {
            classLoader = getClass().getClassLoader();
          }
          Dependencies.findClassDependencies(classLoader, new ClassAcceptor() {
            @Override
            public boolean accept(String className, URL classUrl, URL classPathUrl) {
              try {
                jarOut.putNextEntry(new JarEntry(className.replace('.', '/') + ".class"));
                try (InputStream is = classUrl.openStream()) {
                  ByteStreams.copy(is, jarOut);
                }
              } catch (IOException e) {
                throw Throwables.propagate(e);
              }
              return true;
            }
          }, TwillLauncher.class.getName(), FindFreePort.class.getName());
        }
      }
    });

    LOG.debug("Done {}", Constants.Files.LAUNCHER_JAR);

    localFiles.put(Constants.Files.LAUNCHER_JAR, createLocalFile(Constants.Files.LAUNCHER_JAR, location));
  }

  private void saveClassPaths(Path targetDir) throws IOException {
    Files.write(targetDir.resolve(Constants.Files.APPLICATION_CLASSPATH),
                Joiner.on(':').join(applicationClassPaths).getBytes(StandardCharsets.UTF_8));
    Files.write(targetDir.resolve(Constants.Files.CLASSPATH),
                Joiner.on(':').join(classPaths).getBytes(StandardCharsets.UTF_8));
  }

  private void saveJvmOptions(final Path targetPath) throws IOException {
    if ((extraOptions == null || extraOptions.isEmpty()) &&
      JvmOptions.DebugOptions.NO_DEBUG.equals(debugOptions)) {
      // If no vm options, no need to localize the file.
      return;
    }
    LOG.debug("Creating {}", targetPath);
    JvmOptionsCodec.encode(new JvmOptions(extraOptions, debugOptions), new OutputSupplier<Writer>() {
      @Override
      public Writer getOutput() throws IOException {
        return Files.newBufferedWriter(targetPath, StandardCharsets.UTF_8);
      }
    });
    LOG.debug("Done {}", targetPath);
  }

  private void saveArguments(Arguments arguments, final Path targetPath) throws IOException {
    LOG.debug("Creating {}", targetPath);
    ArgumentsCodec.encode(arguments, new OutputSupplier<Writer>() {
      @Override
      public Writer getOutput() throws IOException {
        return Files.newBufferedWriter(targetPath, StandardCharsets.UTF_8);
      }
    });
    LOG.debug("Done {}", targetPath);
  }

  private void saveEnvironments(Path targetPath) throws IOException {
    if (environments.isEmpty()) {
      return;
    }

    LOG.debug("Creating {}", targetPath);
    try (Writer writer = Files.newBufferedWriter(targetPath, StandardCharsets.UTF_8)) {
      new Gson().toJson(environments, writer);
    }
    LOG.debug("Done {}", targetPath);
  }

  /**
   * Serializes the information for files that are localized to all YARN containers.
   */
  private void createLocalizeFilesJson(Map<String, LocalFile> localFiles) throws IOException {
    LOG.debug("Create and copy {}", Constants.Files.LOCALIZE_FILES);
    Location location = createTempLocation(Constants.Files.LOCALIZE_FILES);

    // Serialize the list of LocalFiles, except the one we are generating here, as this file is used by AM only.
    // This file should never use LocationCache.
    try (Writer writer = new OutputStreamWriter(location.getOutputStream(), StandardCharsets.UTF_8)) {
      new GsonBuilder().registerTypeAdapter(LocalFile.class, new LocalFileCodec())
        .create().toJson(localFiles.values(), new TypeToken<List<LocalFile>>() {
      }.getType(), writer);
    }
    LOG.debug("Done {}", Constants.Files.LOCALIZE_FILES);
    localFiles.put(Constants.Files.LOCALIZE_FILES, createLocalFile(Constants.Files.LOCALIZE_FILES, location));
  }

  private Location copyFromURL(URL url, Location target) throws IOException {
    try (
      InputStream is = url.openStream();
      OutputStream os = new BufferedOutputStream(target.getOutputStream())
    ) {
      ByteStreams.copy(is, os);
    }
    return target;
  }

  private Location createTempLocation(String fileName) {
    String name;
    String suffix = Paths.getExtension(fileName);

    name = fileName.substring(0, fileName.length() - suffix.length() - 1);

    try {
      return appLocation.append(name).getTempFile('.' + suffix);
    } catch (IOException e) {
      throw Throwables.propagate(e);
    }
  }

  /**
   * Returns the context ClassLoader if there is any, otherwise, returns ClassLoader of this class.
   */
  private ClassLoader getClassLoader() {
    ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
    return classLoader == null ? getClass().getClassLoader() : classLoader;
  }

  private ApplicationBundler createBundler(ClassAcceptor classAcceptor) {
    return new ApplicationBundler(classAcceptor).setTempDir(getLocalStagingDir());
  }
}


text:
var React = require('react');
var _ = require('underscore');
var connect = require('react-redux').connect;
var utils = require('../../utils/componentUtils');

// decides what to present as the unit the measure comes in.
// will look a bit ugly but enough to keep moving

function mapStateToProps (state, ownProps) {
  return {
   measures: utils.mapIdsToObjs(ownProps.measureIds, state.Measures),
  };
}

var Basis = React.createClass({
  elements: [],
  componentWillMount: function() {
    var kind = this.props.measure.kind;
    //build the spans
    if(kind === 'numeric') {
      //no iteration nessecary for numeric...
      this.elements.push(

          Unit:
          {this.props.item}

)
     } else if(kind === 'list') {
      //iterate through list and add category labels
      this.elements = _.map(this.props.item, function(definition, i) {
        i++; //start at item 1 not item 0
        return (

            {"Category " + i + ": "}
            {definition}

        )
      }, this);
    } else if (kind === 'qualitative') {
      this.elements.push(

            {"1: "}
            {this.props.measure.scaleDescriptionMin}

)
      this.elements.push(

            {"3: "}
            {this.props.measure.scaleDescriptionMiddle}

)
      this.elements.push(

            {"5: "}
            {this.props.measure.scaleDescriptionMax}

        )
    }
  },
  render: function() {
    return (

        {this.elements}

    )
  }
});

var Measure = React.createClass({
  getBasis: function(measure) {
    if (measure.kind === 'numeric') {
      return measure.unit;
    }
    if (measure.kind === 'qualitative') {
      return _.range(1, 5, 0.5);
    }
    return measure.list;
  },
  render: function() {
    var basis = this.getBasis(this.props.measure);
    var bases = <Basis item={basis} measure={this.props.measure}/>;
    return (

          {bases}

    );
  }
});

var Measures = React.createClass({
  render: function() {
    var measures = _.map(this.props.measures, function(measure) {
      return <Measure measure = {measure} />;
    });
    return (

        {measures}

    );
  }
});

module.exports = connect(mapStateToProps)(Measures);

redpajama_set_name: RedPajamaGithub


text:
package acad.db.courserank.dbis;

/**
 *
 * @author rishabh
 */
public class Header {
    String head_start_text;
    String head_end_text;
    String scripts;
    String title;
    String styleSheets;

    String superheader_start_text;
    String superheader_end_text;

    String header_start_text;
    String picurl, picalt;
    String header_mid_text; //url will be before header_mid_text and alt will be just after it
    String header_end_text1;
    String header_end_text;

    String menubar_start_text;
    String menus[];
    int menuNum;
    String menubar_end_text;

    String onloadJS;
    String searchStr;

    public Header() {
        head_start_text = " ";
        head_end_text = "";
        styleSheets = "<link rel='stylesheet' href='css/index.css' type='text/css' />";
        //scripts = "";
        scripts = "";
        superheader_start_text = "
";
        superheader_end_text = "

"; //Also taken the starting of body which is closed in the footer class

        header_start_text = "

";
	header_end_text += "
<form action='";
        header_end_text1 = "'><input id='search' type='search' name='query' placeholder='Search Anything' /></form>
";
	header_end_text1 += "

";

        menubar_start_text = "

";
        menubar_end_text = "

";
        menuNum = 0;
        menus = new String[2];
        menus[0] = "
Logout
";
	menus[0] += "
Profile
";
        menus[0] += "
Home
";
        menus[1] = "
Register
";
        menus[1] += "
Login
";
        picurl = "images/defaultImage.jpg";
        picalt = "Default Image";
        title = "<title>CourseRank</title>";

        onloadJS = "
";
        searchStr = "search.jsp";
    }

    public void addStyleSheet(String url) {
        styleSheets += "<link rel='stylesheet' href='" + url + "' type='text/css' />";
    }

    public void addScript(String url) {
        scripts += "";
    }

    public void setPicUrl(String url, String alt) {
        picurl = url;
        picalt = alt;
    }

    public void setMenu(int n) {
        menuNum = n;
    }

    public void setTitle(String t) {
        title = "<title>" + t + "</title>";
    }

    public void setSearchTarget(String st) {
        searchStr = st;
    }

    @Override
    public String toString() {
        String result = head_start_text + title + scripts + styleSheets + head_end_text;
        result += superheader_start_text;
        result += header_start_text + picurl + header_mid_text + picalt + header_end_text + searchStr + header_end_text1;
        result += menubar_start_text + menus[menuNum] + menubar_end_text;
        result += superheader_end_text + onloadJS;
        return result;
    }

    public void setOnloadJS(String str) {
        onloadJS = "
";
    }
}

/*

				<input id="search" type="search" placeholder="Search Anything" />

Account

Profile

Home

*/


text:
/* ------------------------------------------------------------------------- */
/*
 *  sockbuf.h
 *
 *  Copyright (c) 2004 - 2010, clown. All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions
 *  are met:
 *
 *    - Redistributions of source code must retain the above copyright
 *      notice, this list of conditions and the following disclaimer.
 *    - Redistributions in binary form must reproduce the above copyright
 *      notice, this list of conditions and the following disclaimer in the
 *      documentation and/or other materials provided with the distribution.
 *    - No names of its contributors may be used to endorse or promote
 *      products derived from this software without specific prior written
 *      permission.
 *
 *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 *  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 *  OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 *  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
 *  TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 *  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 *  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 *  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 *  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 *  Last-modified: Wed 13 Jan 2010 05:36:00 JST
 */
/* ------------------------------------------------------------------------- */
#ifndef CLX_SOCKBUF_H
#define CLX_SOCKBUF_H

#include "config.h"
#include <streambuf>
#include <vector>
#include <cstring>

namespace clx {
	/* --------------------------------------------------------------------- */
	//  basic_sockbuf
	/* --------------------------------------------------------------------- */
	template <
		class Socket,
		class CharT = char,
		class Traits = std::char_traits<CharT>
	>
	class basic_sockbuf : public std::basic_streambuf<CharT, Traits> {
	private:
		typedef std::basic_streambuf<CharT, Traits> super;
	public:
		typedef Socket socket_type;
		typedef CharT char_type;
		typedef Traits traits;
		typedef typename std::vector<CharT> container;
		typedef typename container::size_type size_type;
		typedef typename Traits::int_type int_type;

		enum { npback = 8, nbuf = 65536 };

		// constructor and destructor
		explicit basic_sockbuf(size_type n) :
			super(), sock_(), buffer_(n) {
			this->setg(&buffer_.at(0), &buffer_.at(npback), &buffer_.at(npback));
		}

		explicit basic_sockbuf(socket_type& s, size_type n = nbuf) :
			super(), sock_(&s), buffer_(n) {
			this->setg(&buffer_.at(0), &buffer_.at(npback), &buffer_.at(npback));
		}

		virtual ~basic_sockbuf() throw() {}

		void socket(socket_type& s) { sock_ = &s; }

	protected:
		virtual int_type overflow(int_type c) {
			if (!traits::eq_int_type(c, traits::eof())) {
				char tmp = static_cast<char>(c);
				if (this->xsputn((char_type*)&tmp, 1) == 0) return traits::eof();
			}
			return traits::not_eof(c);
		}

		virtual std::streamsize xsputn(const char_type* s, std::streamsize n) {
			std::streamsize transferred = 0;
			while (transferred < n) {
				int byte = static_cast<int>(n - transferred) * sizeof(char_type);
				int l = sock_->send((char*)s, byte);
				if (l <= 0) break;
				transferred += (l / sizeof(char_type));
				s += (l / sizeof(char_type));
			}
			return transferred;
		}

		virtual int_type underflow() {
			if (this->gptr() == this->egptr()) {
				// copy the previous read string to the putback field
				int n = static_cast<int>(this->gptr() - this->eback());
				if (n > npback) n = npback;
				std::memcpy(&buffer_.at(npback - n), this->gptr() - n, n);

				int byte = static_cast<int>((buffer_.size() - npback) * sizeof(char_type));
				int l = sock_->recv(reinterpret_cast<char*>(&buffer_.at(npback)), byte);
				if (l <= 0) return traits::eof();
				this->setg(&buffer_.at(npback - n), &buffer_.at(npback), &buffer_.at(npback + l));
				return traits::to_int_type(*this->gptr());
			}
			else return traits::eof();
		}

		virtual int_type uflow() {
			int_type c = this->underflow();
			if (!traits::eq_int_type(c, traits::eof())) this->gbump(1);
			return c;
		}

		virtual int sync() {
			return this->overflow(traits::eof());
		}

	private:
		socket_type* sock_;
		container buffer_;
	};
};

#endif // CLX_SOCKBUF_H


text:
var browserName = navigator.appName;
var resourceType = "file";
var ASSOCIATION_TYPE01 = "depends";
var myEditor = null;
var textContentEditor = null;
var textContentUpdator = null;
var filterShown = false;
var isMediationLocalEntrySelected = false;
var emptyErrorMessage

function getParentPath(srcPath){
	var parentPath = srcPath.substr(0, srcPath.lastIndexOf("/"));
	if(parentPath == null || parentPath == ""){
		parentPath = "/";
	}
	return parentPath;
}

function refreshPermissionsSection() {
    sessionAwareFunction(function() {
        var random = getRandom();
        new Ajax.Request('../resources/add_role_for_user.jsp', {
            method: 'get',
            parameters: {path: path,random:random},
            onSuccess: function(transport) {
                var perDiv = $('permissionsDiv');
                perDiv.innerHTML = transport.responseText;
                YAHOO.util.Event.onAvailable('perExpanded', function() {
                    $('perIconExpanded').style.display = "";
                    $('perIconMinimized').style.display = "none";
                    $('perExpanded').style.display = "";
                });
            }
        });
    }, org_wso2_carbon_registry_resource_ui_jsi18n["session.timed.out"]);
}

function showHide(toShowHide) {
    var resource_div = document.getElementById('add-resource-div');
    var folder_div = document.getElementById('add-folder-div');
    var link_div = document.getElementById('add-link-div');
    if (toShowHide == 'add-resource-div') {
        if (resource_div!= null && resource_div.style.display == 'block') {
            resource_div.style.display = 'none';
        }
        else {
            resource_div.style.display = 'block';
            if (folder_div!= null && folder_div.style.display == 'block') folder_div.style.display = 'none';
            if (link_div!=null && link_div.style.display == 'block') link_div.style.display = 'none';
        }
    }

    if (toShowHide == 'add-folder-div') {
        if (folder_div!=null && folder_div.style.display == 'block') {
            folder_div.style.display = 'none';
        }
        else {
            if(folder_div!=null) {
            	folder_div.style.display = 'block';
            }
            if (resource_div!=null && resource_div.style.display == 'block') resource_div.style.display = 'none';
            if (link_div != null && link_div.style.display == 'block') link_div.style.display = 'none';
        }
    }

    if (toShowHide == 'add-link-div') {
        if (link_div!=null && link_div.style.display == 'block') {
            link_div.style.display = 'none';
        }
        else {
            if(link_div!=null){link_div.style.display = 'block';}
            if (resource_div!=null && resource_div.style.display == 'block') resource_div.style.display = 'none';
            if (folder_div !=null && folder_div.style.display == 'block') folder_div.style.display = 'none';
        }
    }
}

//used
function showHideTreeView(path,obj) {
    sessionAwareFunction(function() {
        var stdView = document.getElementById('stdView');
        var treeView = document.getElementById('treeView');
        var random = getRandom();
        var clickedon = obj.id;
        if (stdView && YAHOO.util.Dom.hasClass(stdView,"stdView-notSelected") && clickedon=="stdView") {
            YAHOO.util.Dom.removeClass(stdView,"stdView-notSelected");
            YAHOO.util.Dom.addClass(stdView,"stdView-Selected");

            YAHOO.util.Dom.removeClass(treeView,"treeView-Selected");
            YAHOO.util.Dom.addClass(treeView,"treeView-notSelected");
            new Ajax.Updater('viewPanel', '../hdfsmgt/standard_view_ajaxprocessor.jsp', { method: 'get', parameters: {path: path,random:random}, evalScripts:true });

        } else if(treeView && YAHOO.util.Dom.hasClass(treeView,"treeView-notSelected") && clickedon=="treeView") {
            YAHOO.util.Dom.removeClass(treeView,"treeView-notSelected");
            YAHOO.util.Dom.addClass(treeView,"treeView-Selected");

            YAHOO.util.Dom.removeClass(stdView,"stdView-Selected");
            YAHOO.util.Dom.addClass(stdView,"stdView-notSelected");
            new Ajax.Updater('viewPanel', '../hdfsmgt/tree_view_ajaxprocessor.jsp', { method: 'get', parameters: {path: path, treeNavigationPath: path, reference: "compute",random:random}, evalScripts:true });
        }
    },org_wso2_carbon_hdfs_mgt_ui_jsi18n["session.timed.out"]);
}

function setTreeNavigationPath(treeNavigationPath, reference) {
    new Ajax.Request('../hdfsmgt/tree_view_ajaxprocessor.jsp',
    {
        method:'get',
        parameters: {treeNavigationPath: treeNavigationPath, reference: reference, random:getRandom()},

        onSuccess: function() {
        },

        onFailure: function() {
        }
    });
}

function setStyleDisplayNone(div)
{
	if(div != null)
		{
			div.style.display = "none";
		}
}

function hideOthers(id, type) {

    var renamePanel = document.getElementById("rename_panel" + id);
    var createPanel = document.getElementById("create_panel" + id);
    var uploadPanel = document.getElementById("upload_panel" + id);
    var symLinkPanel = document.getElementById("symlinkContentUI" + id);

    setStyleDisplayNone(renamePanel);
    setStyleDisplayNone(createPanel);
    setStyleDisplayNone(uploadPanel);
    setStyleDisplayNone(symLinkPanel);

    if(type != 'del'){
		var panelToView = document.getElementById(type + id);
	    panelToView.style.display = "";
		}
}

function renameItem(srcPath, resourceEditDivID, isFolder, pageNumber) {
		sessionAwareFunction(function() {
         var reason = "";
         var enteredvalue = ltrim(document.getElementById(resourceEditDivID).value);
        document.getElementById(resourceEditDivID).value = enteredvalue;
        if(enteredvalue == ""){
        	reason = "The value entered is empty";
        }
       if (reason == "") {
            reason += validateIllegal(resourceEditDivID);
        }
        if (reason == "") {
            reason += validateResourcePathAndLength(document.getElementById(resourceEditDivID));
        }
        var parentPath = getParentPath(srcPath);
        var resourcePathPrefix = parentPath;
        if(parentPath != "/"){
        	resourcePathPrefix = parentPath + "/";
        }
        var resourcePath= resourcePathPrefix + document.getElementById(resourceEditDivID).value;
        resourcePath = resourcePath.replace("//", "/");
        if (reason != "") {
            CARBON.showWarningDialog(reason, function(){
            	reActivateRenameButton(resourceEditDivID);
            	});
            return false;
          }else {
            var successMsg = org_wso2_carbon_hdfs_mgt_ui_jsi18n["file.rename.successful"];
            if(isFolder)
            	{
            		successMsg = org_wso2_carbon_hdfs_mgt_ui_jsi18n["folder.rename.successful"];
            	}

            new Ajax.Request('../hdfsmgt/rename_item_ajaxprocessor.jsp',
            {
                method:'post',
                parameters: {srcPath: srcPath, dstPath: resourcePath, isFolder: isFolder},

                onSuccess: function() {
                	 CARBON.showInfoDialog(successMsg, function(){
                		 fillContentSection(parentPath, pageNumber)
            		 });

                },

                onFailure: function(transport) {
                	   CARBON.showErrorDialog(trim(transport.responseText),function(){
                		   window.location = "../hdfsmgt/hdfs_dashboard.jsp?region=region3&item=resource_browser_menu&viewType=std&path=" + getParentPath(srcPath);
                	          return;
                	      });
                }
            });
        }
		},org_wso2_carbon_hdfs_mgt_ui_jsi18n["session.timed.out"]);
    loadData();
    return true;

}

function reActivateRenameButton(renameDivId){
	document.getElementById(renameDivId+"Button").disabled = false;
}

function validateIllegal(fld){
    var error = "";
    //var illegalChars = /([^a-zA-Z0-9_\-\x2E\&\?\/\:\,\s\(\)\[\]])/;
    var illegalChars = /([~!@#;%^*+={}\|\\<>\"\',])/; // disallow ~!@#$;%^*+={}|\<>"',
    var illegalCharsInput = /(\<[a-zA-Z0-9\s\/]*>)/;
    if (illegalChars.test(fld.value) || illegalCharsInput.test(fld.value)) {
        error = "illegal input"
    }
    return error;
}

function createFolder(srcPath, createDivID, pageNumber)
{
	sessionAwareFunction(function() {
	var newName = document.getElementById(createDivID).value;
	var newFolder;
	if(srcPath == "/")
	{
		newFolder = srcPath + newName;
	}else
	{
		newFolder = srcPath+'/'+newName;
	}

	new Ajax.Request('../hdfsmgt/create_folder_ajaxprocessor.jsp',
	         {
	                method:'post',
	                parameters: {folderPath: newFolder},

	                onSuccess: function() {
	                	 CARBON.showInfoDialog(org_wso2_carbon_hdfs_mgt_ui_jsi18n["folder.create.successful"], function(){
	                		 fillContentSection(srcPath, pageNumber)
	                		 });

	                },

	                onFailure: function(transport) {
	                	   CARBON.showErrorDialog(trim(transport.responseText),function(){
	                		   window.location = "../hdfsmgt/hdfs_dashboard.jsp?region=region3&item=resource_browser_menu&viewType=std&path=" + getParentPath(srcPath);
	                	          return;
	                	      });
	                }
	          });
    },org_wso2_carbon_hdfs_mgt_ui_jsi18n["session.timed.out"]);
}

function deleteFolder(folderToDelete, pageNumber) {

	 CARBON.showConfirmationDialog(org_wso2_carbon_hdfs_mgt_ui_jsi18n["folder.delete.confirmation"], function(){
		    new Ajax.Request('../hdfsmgt/delete_folder_ajaxprocessor.jsp',
	       	         {
		    				method:'post',
	       	                parameters: {folderPath: folderToDelete},

	       	                onSuccess: function() {
	       	                	CARBON.showInfoDialog(org_wso2_carbon_hdfs_mgt_ui_jsi18n["folder.delete.successful"], function(){
	   	                		 fillContentSection(getParentPath(folderToDelete), pageNumber)
		                		 });

	       	                },

	       	                onFailure: function(transport) {
	       	                 CARBON.showErrorDialog(trim(transport.responseText),function(){
		                		   window.location = "../hdfsmgt/hdfs_dashboard.jsp?region=region3&item=resource_browser_menu&viewType=std&path=" + getParentPath(folderToDelete);
		                	          return;
		                	      });
	       	                }
	       	            });
			 },org_wso2_carbon_hdfs_mgt_ui_jsi18n["session.timed.out"]);
	 loadData();
}

function deleteFile(filePath, pageNumber) {

	CARBON.showConfirmationDialog(org_wso2_carbon_hdfs_mgt_ui_jsi18n["file.delete.confirmation"], function(){
	new Ajax.Request('../hdfsmgt/delete_file_ajaxprocessor.jsp',
  	         {
   				method:'post',
  	                parameters: {filePath: filePath},

  	                onSuccess: function() {
  	                	CARBON.showInfoDialog(org_wso2_carbon_hdfs_mgt_ui_jsi18n["file.delete.successful"], function(){
	                		 fillContentSection(getParentPath(filePath), pageNumber)
               		 });

  	                },

  	                onFailure: function(transport) {
  	                  CARBON.showErrorDialog(trim(transport.responseText),function(){
               		   window.location = "../hdfsmgt/hdfs_dashboard.jsp?region=region3&item=resource_browser_menu&viewType=std&path=" + getParentPath(filePath);
               	          return;
               	      });
  	                }
  	            });
	 },org_wso2_carbon_hdfs_mgt_ui_jsi18n["session.timed.out"]);
	 loadData();
}

function fillFileUploadDetails(formname) {

    var filepath = document.forms[formname].uploadFile.value;

    var filename = "";
    if (filepath.indexOf("\\") != -1) {
        filename = filepath.substring(filepath.lastIndexOf('\\') + 1, filepath.length);
    } else {
        filename = filepath.substring(filepath.lastIndexOf('/') + 1, filepath.length);
    }
    document.forms[formname].uploadedFileName.value = filename;
}

function submitUploadUpdatedContentForm() {

    var rForm = document.forms["updateUploadForm"];
    /* Validate the form before submit */

    var reason = "";
    if (rForm.upload.value.length == 0) {
        reason = "Empty value entered."
    }

    if (reason != "") {
        CARBON.showWarningDialog(reason);
        return false;
    } else {
        rForm.submit();
    }
    return true;
}

function submitUploadContentForm(formName) {
   sessionAwareFunction(function() {
	    var rForm = document.forms[formName];
	    var file = document.forms[formName].uploadFile.value;
		var fileName = document.forms[formName].uploadedFileName.value;
		var reason = "";

        if(file.length == 0){
        	reason +="No file has been selected" +"
";
        }
        else if (fileName.length == 0) {
            reason +=  "File name not entered" + "
";
        }

        if (reason != "") {
           document.getElementById('whileUpload').style.display = "none";
            CARBON.showWarningDialog(reason);
            return false;
        } else {
        	rForm.submit();
         }
            }, org_wso2_carbon_hdfs_mgt_ui_jsi18n["session.timed.out"]);
    return true;
}

function whileUpload(){
    if(document.getElementById('whileUpload')!=null){
    	document.getElementById('whileUpload').style.display = "";
    }

}
//function submitSymlinkContentForm() {
//
//    if(!validateTextForIllegal(document.forms["symlinkContentForm1"].targetpath)){
//      CARBON.showWarningDialog(org_wso2_carbon_registry_common_ui_jsi18n["the"] + " "+ "symlink content"+" " + org_wso2_carbon_registry_common_ui_jsi18n["contains.illegal.chars"]);
//        document.getElementById('add-link-div').style.display = "";
//        document.getElementById('whileUpload').style.display = "none";
//      return false;
//    }
//
//    sessionAwareFunction(function() {
//        var rForm = document.forms["symlinkContentForm1"];
//
//        /* Validate the form before submit */
//        var reason = "";
//        reason += validateEmpty(rForm.filename, org_wso2_carbon_hdfs_mgt_ui_jsi18n["name"]);
//        if (reason == "") {
//            reason += validateForInput(rForm.filename, org_wso2_carbon_hdfs_mgt_ui_jsi18n["name"]);
//        }
//        if (reason == "") {
//            reason += validateIllegal(rForm.filename, org_wso2_carbon_hdfs_mgt_ui_jsi18n["name"]);
//        }
//        if (reason == "") {
//            reason += validateResourcePathAndLength(rForm.filename);
//        }
//        if (reason == "") {
//            reason += validateEmpty(rForm.targetpath, org_wso2_carbon_hdfs_mgt_ui_jsi18n["path"]);
//        }
//        if (reason == "" && rForm.targetpath.value == '/') {
//            reason += org_wso2_carbon_hdfs_mgt_ui_jsi18n["unable.create.symlink.to.root"];
//        }
//        var resourcePath= rForm.path.value + '/' + rForm.filename.value;
//        resourcePath = resourcePath.replace("//", "/");
//
//        if (reason != "") {
//            CARBON.showWarningDialog(reason);
//            document.getElementById('whileUpload').style.display = "none";
//            return false;
//        }
//
//        var parentPath = document.getElementById('srParentPath').value;
//        var name = document.getElementById('srFileName').value;
//        var targetPath = document.getElementById('srPath').value;
//        new Ajax.Request('../resources/add_symlink_ajaxprocessor.jsp',
//        {
//            method:'post',
//            parameters: {parentPath: parentPath, name: name, targetPath: targetPath,random:getRandom()},
//
//            onSuccess: function() {
//                refreshMetadataSection(parentPath);
//                refreshContentSection(parentPath);
//                document.getElementById('whileUpload').style.display = "none";
//                CARBON.showInfoDialog(org_wso2_carbon_hdfs_mgt_ui_jsi18n["successfully.created.symbolic.link"],loadData);
//            },
//
//            onFailure: function(transport) {
//                document.getElementById('whileUpload').style.display = "none";
//                CARBON.showErrorDialog(org_wso2_carbon_hdfs_mgt_ui_jsi18n["failed.to.create.symbolic.link"] + transport.responseText,loadData);
//            }
//        });
//    }, org_wso2_carbon_hdfs_mgt_ui_jsi18n["session.timed.out"]);
//    return true;
//}

function fillContentSection(path, pageNumber, viewMode, consumerID,
		targetDivID, iperPg) {
	var itemsPerPage = iperPg == null ? 10 : iperPg;
	var random = getRandom();
	window.location = "../hdfsmgt/hdfs_dashboard.jsp?region=region3&item=hdfs_list_menu&viewType=std&path="
			+ path + "&requested_page=" + pageNumber + "&itemCount=" + itemsPerPage;
}

function showHDFSTreeWithLoadFunction(loadFunction, textBoxId, onOKCallback, rootPath, relativeRoot, displayRootPath) {

    //This value is passed to address the cashing issue in IE
    var loadingContent = '
  ' + "HDFS Folder Tree Loading"+ '
';
    CARBON.showPopupDialog(loadingContent, "HDFS Folder Tree", 500, false);
    var random = getRandom();
    if (onOKCallback) {
        if (typeof onOKCallback == "function") {
            onOKCallback = onOKCallback.toString().substring('function '.length);
            onOKCallback = onOKCallback.substring(0, onOKCallback.indexOf("("));
            if (rootPath) {
                if (relativeRoot) {
                    new Ajax.Request('../hdfsmgt/hdfs_tree_ajaxprocessor.jsp', {
                        method: 'get',
                        parameters: {textBoxId:textBoxId,rootPath:rootPath,relativeRoot:relativeRoot,displayRootPath:displayRootPath,onOKCallback:onOKCallback,random:random},
                        onSuccess: function(transport) {
                            var dialog = $('dialog');
                            dialog.innerHTML = transport.responseText;
                            if (loadFunction) {
                                loadSubTree(rootPath, 'root', textBoxId, 'false');
                            }
                        }
                    });
                } else {
                    new Ajax.Request('../hdfsmgt/hdfs_tree_ajaxprocessor.jsp', {
                        method: 'get',
                        parameters: {textBoxId:textBoxId,rootPath:rootPath,displayRootPath:displayRootPath,onOKCallback:onOKCallback,random:random},
                        onSuccess: function(transport) {
                            var dialog = $('dialog');
                            dialog.innerHTML = transport.responseText;
                            if (loadFunction) {
                                loadSubTree(rootPath, 'root', textBoxId, 'false');
                            }
                        }
                    });
                }
            } else {
                new Ajax.Request('../hdfsmgt/hdfs_tree_ajaxprocessor.jsp', {
                    method: 'get',
                    parameters: {textBoxId:textBoxId,onOKCallback:onOKCallback,random:random},
                    onSuccess: function(transport) {
                        var dialog = $('dialog');
                        dialog.innerHTML = transport.responseText;
                        if (loadFunction) {
                            loadSubTree('/', 'root', textBoxId, 'false');
                        }
                    }
                });
            }
        } else {
            if (relativeRoot) {
                new Ajax.Request('../hdfsmgt/hdfs_tree_ajaxprocessor.jsp', {
                    method: 'get',
                    parameters: {textBoxId:textBoxId,rootPath:rootPath,relativeRoot:relativeRoot,displayRootPath:displayRootPath,random:random},
                    onSuccess: function(transport) {
                        var dialog = $('dialog');
                        dialog.innerHTML = transport.responseText;
                        if (loadFunction) {
                            loadSubTree(rootPath, 'root', textBoxId, 'false');
                        }
                    }
                });
            } else {
                if (!rootPath) {
                    rootPath = onOKCallback;
                }
                new Ajax.Request('../hdfsmgt/hdfs_tree_ajaxprocessor.jsp', {
                    method: 'get',
                    parameters: {textBoxId:textBoxId,rootPath:rootPath,displayRootPath:displayRootPath,random:random},
                    onSuccess: function(transport) {
                        var dialog = $('dialog');
                        dialog.innerHTML = transport.responseText;
                        if (loadFunction) {
                            loadSubTree(rootPath, 'root', textBoxId, 'false');
                        }
                    }
                });
            }
        }
        //new Ajax.Updater('dialog', '../hdfsmgt/hdfs_tree_ajaxprocessor.jsp', { method: 'get', parameters: {textBoxId:textBoxId,onOKCallback:onOKCallback,random:random} });
    } else {
        new Ajax.Request('../hdfsmgt/hdfs_tree_ajaxprocessor.jsp', {
            method: 'get',
            parameters: {textBoxId:textBoxId,random:random},
            onSuccess: function(transport) {
                var dialog = $('dialog');
                dialog.innerHTML = transport.responseText;
                if (loadFunction) {
                    loadSubTree('/', 'root', textBoxId, 'false');
                }
            }
        });
        //new Ajax.Updater('dialog', '../hdfsmgt/hdfs_tree_ajaxprocessor.jsp', { method: 'get', parameters: {textBoxId:textBoxId,random:random} });
    }
}

function showHDFStree(textBoxId, onOKCallback, rootPath) {
    showHDFSTreeWithLoadFunction(true, textBoxId, onOKCallback, rootPath, null, "false");
}

function showCollectionTreeWithLoadFunction(loadFunction, textBoxId, onOKCallback, rootPath, relativeRoot) {

    //This value is passed to address the cashing issue in IE
    var loadingContent = '
  ' + org_wso2_carbon_hdfs_mgt_ui_jsi18n["resource.tree.loading"] + '
';
    CARBON.showPopupDialog(loadingContent, org_wso2_carbon_hdfs_mgt_ui_jsi18n["resource.tree"], 500, false);
    var random = getRandom();
    if (onOKCallback) {
        if (typeof onOKCallback == "function") {
            onOKCallback = onOKCallback.toString().substring('function '.length);
            onOKCallback = onOKCallback.substring(0, onOKCallback.indexOf("("));
            if (rootPath) {
                if (relativeRoot) {
                new Ajax.Request('../hdfsmgt/hdfs_tree_ajaxprocessor.jsp', {
                    method: 'get',
                     parameters: {textBoxId:textBoxId,rootPath:rootPath,relativeRoot:relativeRoot,onOKCallback:onOKCallback,random:random,hideResources:'true'},
                    onSuccess: function(transport) {
                        var dialog = $('dialog');
                        dialog.innerHTML = transport.responseText;
                        if (loadFunction) {
                            loadSubTree(rootPath, 'root', textBoxId, 'true');
                        }
                    }
                });
            } else {
                new Ajax.Request('../hdfsmgt/hdfs_tree_ajaxprocessor.jsp', {
                    method: 'get',
                     parameters: {textBoxId:textBoxId,rootPath:rootPath,onOKCallback:onOKCallback,random:random,hideResources:'true'},
                         onSuccess: function(transport) {
                             var dialog = $('dialog');
                             dialog.innerHTML = transport.responseText;
                             if (loadFunction) {
                                 loadSubTree(rootPath, 'root', textBoxId, 'true');
                             }
                         }
                     });
                 }
             } else {
                 new Ajax.Request('../hdfsmgt/hdfs_tree_ajaxprocessor.jsp', {
                     method: 'get',
                    parameters: {textBoxId:textBoxId,onOKCallback:onOKCallback,random:random,hideResources:'true'},
                    onSuccess: function(transport) {
                        var dialog = $('dialog');
                        dialog.innerHTML = transport.responseText;
                        if (loadFunction) {
                            loadSubTree('/', 'root', textBoxId, 'true');
                        }
                    }
                });
            }
        } else {
            if (relativeRoot) {
                new Ajax.Request('../hdfsmgt/hdfs_tree_ajaxprocessor.jsp', {
                    method: 'get',
                    parameters: {textBoxId:textBoxId,rootPath:rootPath,relativeRoot:relativeRoot,random:random,hideResources:'true'},
                    onSuccess: function(transport) {
                        var dialog = $('dialog');
                        dialog.innerHTML = transport.responseText;
                        if (loadFunction) {
                            loadSubTree(rootPath, 'root', textBoxId, 'true');
                        }
                    }
                });
            } else {
                if (!rootPath) {
                    rootPath = onOKCallback;
                }
                new Ajax.Request('../hdfsmgt/hdfs_tree_ajaxprocessor.jsp', {
                    method: 'get',
                    parameters: {textBoxId:textBoxId,rootPath:rootPath,random:random,hideResources:'true'},
                    onSuccess: function(transport) {
                        var dialog = $('dialog');
                        dialog.innerHTML = transport.responseText;
                        if (loadFunction) {
                            loadSubTree(rootPath, 'root', textBoxId, 'true');
                        }
                    }
                });
            }
        }
        //new Ajax.Updater('dialog', '../hdfsmgt/hdfs_tree_ajaxprocessor.jsp', { method: 'get', parameters: {textBoxId:textBoxId,onOKCallback:onOKCallback,random:random,hideResources:'true'} });
    } else {
        new Ajax.Request('../hdfsmgt/hdfs_tree_ajaxprocessor.jsp', {
            method: 'get',
            parameters: {textBoxId:textBoxId,random:random,hideResources:'true'},
            onSuccess: function(transport) {
                var dialog = $('dialog');
                dialog.innerHTML = transport.responseText;
                if (loadFunction) {
                    loadSubTree('/', 'root', textBoxId, 'true');
                }
            }
        });
        //new Ajax.Updater('dialog', '../hdfsmgt/hdfs_tree_ajaxprocessor.jsp', { method: 'get', parameters: {textBoxId:textBoxId,random:random,hideResources:'true'} });
    }
}

function showCollectionTree(textBoxId, onOKCallback, rootPath) {
    showCollectionTreeWithLoadFunction(true, textBoxId, onOKCallback, rootPath);
}

function pickPath(path, textBoxId, reference, isfolder) {
    if (!textBoxId || textBoxId == 'null') {
        setTreeNavigationPath(path, reference);
        window.location = "../hdfsmgt/hdfs_dashboard.jsp?region=region3&item=resource_browser_menu&viewType=std&path=" + path.replace(/&/g, "%26")+"&isfolder="+isfolder;
        return;
    }
    document.getElementById('pickedPath').value = path;
    document.getElementById('pickedPath').focus();
    //document.getElementById(textBoxId).value = document.getElementById('pickedPath').value;
    //This is a hack to fix the ie7 issue with not getting the object using it's id
    //if (textBoxId == "associationPaths") document.forms.assoForm.associationPaths.value = document.getElementById('pickedPath').value;
}

function loadSubTree(path, parentId, textBoxId, hideResources, callback) {
    var theImg = document.getElementById("plus_" + parentId);
    sessionAwareFunction(function() {
        if (theImg) {
            var theDiv = document.getElementById('child_' + parentId);
            if (theDiv) {
                theDiv.style.display = '';
            }
            var isPlus = theImg.style.display != 'none';
            if (isPlus) {
                while (path.indexOf(" ") > 0) {
                    path = path.replace(" ", "%20")
                }
                while (path.indexOf("&") > 0) {
                    path = path.replace(/&/g, "%26")
                }
                var url = '../hdfsmgt/hdfs_sub_tree_ajaxprocessor.jsp?path=' + path + '&parentId=' + parentId + '&textBoxId=' + textBoxId + (hideResources == 'true' ? '&hideResources=true' : '');
                jQuery("#child_" + parentId).load(url, null,
                        function(data, status, t) {
                            if (status != "success") {
                                //CARBON.showWarningDialog(org_wso2_carbon_hdfs_mgt_ui_jsi18n["error.occured"]);
                                document.getElementById("local-registry-td").style.display = "none";
                            }
                            if (callback && typeof callback == "function") {
                                callback();
                            }
                            // Add necessary logic to handle these scenarios if needed
                            if (data || t) {}
                        });
            } else {
                if (theDiv) {
                    theDiv.innerHTML = "";
                    theDiv.style.display = 'none';
                }
            }
            showHideCommon('plus_' + parentId);
            showHideCommon('minus_' + parentId);
        }

    }, org_wso2_carbon_hdfs_mgt_ui_jsi18n["session.timed.out"]);
}

function getWindowWidth() {
    var crossWidth = 0; //crossHeight = 0;
    if (typeof( window.innerWidth ) == 'number') {
        //Non-IE
        crossWidth = window.innerWidth;
        //crossHeight = window.innerHeight;
    } else if (document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight )) {
        //IE 6+ in 'standards compliant mode'
        crossWidth = document.documentElement.clientWidth;
        //crossHeight = document.documentElement.clientHeight;
    } else if (document.body && ( document.body.clientWidth || document.body.clientHeight )) {
        //IE 4 compatible
        crossWidth = document.body.clientWidth;
        //crossHeight = document.body.clientHeight;
    }
    return(crossWidth);
}

function fixResourceTable() {
    var wdWidth = getWindowWidth();
    var colWidth = (wdWidth * 65) / 100 - 560;
    if (document.getElementById('resourceSizer') == null) return;

    document.getElementById('resourceSizer').width = colWidth + "px";

    //Calculate the truncate length for each resource name
    var truncateLength = parseInt(colWidth / 12 + (wdWidth - 1016) / 9);

    //Truncate the resource names by getting there elements by class name
    /*var classnameRef = "__resourceNameRef";*/
    var classname = "__resourceName";

    var node = document.getElementById("entryList");

    var re = new RegExp('\\b' + classname + '\\b');
    var els = node.getElementsByTagName("*");
    var elsRef = node.getElementsByTagName("*");

    for (var i = 0,j = els.length; i < j; i++) {

        if (re.test(els[i].className)) els[i].innerHTML = elsRef[i - 1].innerHTML.truncate(truncateLength);

    }

}

function validateWSDL() {
    showHideCommon('validationDiv');
}

function loadResourcePage(path, viewMode, consumerID, targetDiv) {
    return loadJSPPage('hdfs_dashboard', path, viewMode, consumerID, targetDiv);
}

//private
function loadJSPPage(pagePrefixName, path, viewMode, consumerID, targetDiv) {

    if (viewMode != undefined && viewMode != null && 'inlined' == viewMode) {

        var suffix = "&resourceViewMode=inlined";
        if (consumerID != undefined && consumerID != null && consumerID != "") {
            suffix += "&resourcePathConsumer=" + consumerID;
        }

        if (targetDiv == null || targetDiv == undefined || targetDiv == "" || targetDiv == "null") {
            targetDiv = 'registryBrowser';
        }

        suffix += "&targetDivID=" + targetDiv;

        if (path == "#") {
            path = '/';
        }
        path = path.replace(/&/g, "%26");

        var url = '../hdfsmgt/' + pagePrefixName + '_ajaxprocessor.jsp?path=' + path + suffix;

        jQuery("#popupContent").load(url, null,
                function(res, status, t) {
                    if (status != "success") {
                        //CARBON.showWarningDialog(org_wso2_carbon_hdfs_mgt_ui_jsi18n["error.occured"]);
                    }
                    // Add necessary logic to handle these scenarios if needed
                    if (res || t) {}
                });
        return false;
    } else {
        path = path.replace(/&/g, "%26");
        if (pagePrefixName == 'resource') {
            document.location.href = '../hdfsmgt/' + pagePrefixName + '.jsp?region=region3&item=hdfs_list_menu&path=' + path + "&screenWidth=" + screen.width;
        } else {
            document.location.href = '../hdfsmgt/' + pagePrefixName + '.jsp?path=' + path + "&screenWidth=" + screen.width;

        }
    }
    return true;
}

function setPathURL(divNum)
{
	onceDone = 0;
	j = 2;
	fullString = "";

	screenWidth = screen.width;

	switch (screenWidth)
	{
	case 1024:
	  maxChars = 90;
	  break;
	default:
	  maxChars = screenWidth / 1024 * 90;
	}

	while (j < divNum)
	{
		divID = "pathResult" + j;
		fullString = fullString + document.getElementById(divID).innerHTML;

		if ((fullString.length > maxChars) && (onceDone<8))
		{
			document.getElementById(divID).innerHTML = "
"+document.getElementById(divID).innerHTML;
			onceDone = onceDone + 1;
            fullString = "";
		}
		j = j + 1;
	}

}

function showInLinedRegistryBrowser(id) {
    showInLinedRegistryBrowserOnDiv('registryBrowser', id);
}

function showInLinedRegistryBrowserOnDiv(divID, id) {
	//TODO check on this

    if (id == null || id == undefined || id == "") {
        CARBON.showInfoDialog(org_wso2_carbon_hdfs_mgt_ui_jsi18n["registry.key.cannot.be.null.or.empty"]);
    }

    var url = '../hdfsmgt/hdfs_tree_ajaxprocessor.jsp?resourceViewMode=inlined&resourcePathConsumer=' + id + '&targetDivID=' + divID + '&random=' + random + '&textBoxId=' + id;

      //This value is passed to address the cashing issue in IE
    var loadingContent = '
  ' + org_wso2_carbon_hdfs_mgt_ui_jsi18n["resource.tree.loading"] + '
';
    CARBON.showPopupDialog(loadingContent, org_wso2_carbon_hdfs_mgt_ui_jsi18n["resource.tree"], 500, false);
    var random = getRandom();
    new Ajax.Updater('dialog', url, { method: 'get', parameters:{random:getRandom()} ,evalScripts:true});
}

function hideInLinedRegistryBrowser(divID) {

    if (divID == null || divID == undefined || divID == "" || divID == "null") {
        divID = 'registryBrowser';
    }
    var nsDiv = document.getElementById(divID);
    if (nsDiv != null && nsDiv != undefined) {
        nsDiv.style.display = "none";
        nsDiv.innerHTML = "";
    }

    return false;
}

function setResolvedResourcePathOnConsumer(consumerID, synapseRoot) {

    if (consumerID == undefined || consumerID == null || "" == consumerID) {
        return false;
    }

    var path = document.getElementById("pickedPath").value;

    if (path == null || path == undefined || path == "" || path == "null") {
        return false;
    }
    var suffix = "&resourceViewMode=inlined";
    if (synapseRoot != undefined && synapseRoot != null && synapseRoot != "") {
        suffix += "&synapseroot=" + synapseRoot;
    } else {
        return false;
    }

    if (path == "#") {
        path = '/';
    }

    var url = '../resources/save-path_ajaxprocessor.jsp?path=' + path + suffix;

    jQuery.get(url, ({}),
            function(data, status) {
                if (status != "success") {
                    CARBON.showWarningDialog(org_wso2_carbon_hdfs_mgt_ui_jsi18n["error.occured"]);
                } else {
                    setResourcePathOnConsumer(consumerID, trim(data));
                }
            });
    return false;
}
function ltrim(str) {
    for (var k = 0; k < str.length && str.charAt(k) <= " "; k++) {}
    return str.substring(k, str.length);
}
function rtrim(str) {
    for (var j = str.length - 1; j >= 0 && str.charAt(j) <= " "; j--) {}
    return str.substring(0, j + 1);
}

//This function accepts a String and trims the string in both sides of the string ignoring space characters
function trim(stringValue) {
    return ltrim(rtrim(stringValue));
}

function showHideCommon(divId){
	var theDiv = document.getElementById(divId);
	if(theDiv.style.display=="none"){
		theDiv.style.display="";
	}else{
		theDiv.style.display="none";
	}
}
function blockManual(e){
    if (e) {
        //handle-event logic
    }
	var path=document.getElementById('uResourceFile');
	 return (path.value.length > 2);
}
function handleRichText(){
	var radioObj = document.textContentForm.richText;
	var selected = "";
	for(var i=0;i<radioObj.length;i++){
		if(radioObj[i].checked)selected = radioObj[i].value;
	}

	var textAreaPanel = $('textAreaPanel');
	var trPlainContent = $('trPlainContent');
	var content = "";
	if (textContentEditor) {
	        textContentEditor.saveHTML();
	        content = textContentEditor.get('textarea').value;
	}
	if(selected=="plain"){
		trPlainContent.style.display = "";
		textAreaPanel.style.display = "none";
		trPlainContent.value=textContentEditor.get('textarea').value;
	}
	if(selected=="rich"){
		trPlainContent.style.display = "none";
		textAreaPanel.style.display = "";
		textContentEditor.setEditorHTML(trPlainContent.value);
		textContentEditor.saveHTML();
		textContentEditor.render();
	}
}

function handleUpdateRichText(){
    var radioObj = new Array();
    radioObj[0] = $('editTextContentIDRichText0');
    radioObj[1] = $('editTextContentIDRichText1');
	var selected = "";
	for(var i=0;i<radioObj.length;i++){
		if(radioObj[i].checked)selected = radioObj[i].value;
	}

	var textAreaPanel = $('editTextContentTextAreaPanel');
	var trPlainContent = $('editTextContentIDPlain');
	var content = "";
	if (textContentUpdator) {
	        textContentUpdator.saveHTML();
	        content = textContentUpdator.get('textarea').value;
	}
	if(selected=="plain"){
		trPlainContent.style.display = "";
		textAreaPanel.style.display = "none";
  		trPlainContent.value=textContentUpdator.get('textarea').value;
	}
	if(selected=="rich"){
		trPlainContent.style.display = "none";
		textAreaPanel.style.display = "";
		textContentUpdator.setEditorHTML(trPlainContent.value);
		textContentUpdator.saveHTML();
		textContentUpdator.render();
	}
}

function loadActionPane(rowNum,type){

        var clickedTD = "actionPaneHelper"+rowNum;
        var actionLink = type+"Link"+rowNum;
        var toShow=type+"Pane"+rowNum;
        var todo = "toShow";
        if($(toShow).style.display != "none"){
        	todo ="toHide";
        }
	var allElms = document.getElementById("entryList").getElementsByTagName("*");

	for (var i = 0; i < allElms.length; i++) {
		if(YAHOO.util.Dom.hasClass(allElms[i], "actionPaneSelector")){
			allElms[i].style.display="none";
		}
		if(YAHOO.util.Dom.hasClass(allElms[i], "action-pane-helper")){
			YAHOO.util.Dom.removeClass(allElms[i],"actionSelected");
		}
		if(YAHOO.util.Dom.hasClass(allElms[i], "entryName-expanded")){
			YAHOO.util.Dom.removeClass(allElms[i],"entryName-expanded");
		}
		if(YAHOO.util.Dom.hasClass(allElms[i], "copy-move-panel")){
			allElms[i].style.display="none";
		}
	}
	if(todo == "toShow"){
		YAHOO.util.Dom.addClass(clickedTD,"actionSelected");
		$(toShow).style.display = "";

		YAHOO.util.Dom.addClass(actionLink,"entryName-expanded");
	}
	else{
		$(toShow).style.display = "none";

		YAHOO.util.Dom.addClass(actionLink,"entryName-contracted");
	}
}
function validateResourcePathAndLength(fld) {

    var error = "";
    var regx = RegExp("(//)");
    if (fld.value.match(regx)) {
        error = "The given name" + " '" + fld.value + "' " + "is not a valid path" + "
";
    } else if (fld.value.length > 256) {
        error = "The given name" + " '" + fld.value + "' " + " is too long" + "
";
    }
    return error;
}

function validateDescriptionLength(fld) {

    var error = "";
    if (fld.value.length > 1000) {
        error = org_wso2_carbon_hdfs_mgt_ui_jsi18n["the.given.description"] + " '" + fld.value + "' " + org_wso2_carbon_hdfs_mgt_ui_jsi18n["cannot.contain.more.than2"] + "
";
    }
    return error;
}

function loadFromPath() {
    var pickedPath = $('uLocationBar');
    var pathValue = null;
    if(pickedPath != null || pickedPath != "")
    	{
   // 		pathvalue = pickedPath.value.replace(/^\s\s*/, '').replace(/\s\s*$/, '').replace(/&/g, "%26");
    	}
    window.location = "../hdfsmgt/hdfs_dashboard.jsp?region=region3&item=resource_browser_menu&viewType=std&path=" + pickedPath.value;
}

function validateProvidedResoucePath(pickedPath, errorMessage) {
    var reason = "";
	var result = false;

	pickedPath.value = pickedPath.value.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
	if (pickedPath.value.length == 0) {
	        reason = errorMessage + "
";
	}
	if(reason ==""){
        new Ajax.Request('../resources/resource_exists_ajaxprocessor.jsp',
        {
            method:'post',
            parameters: {pickedPath: pickedPath.value,random:getRandom()},
            asynchronous:false,
            onSuccess: function(transport) {
                var returnValue = transport.responseText;
                if (returnValue.search(/----ResourceExists----/) == -1){
                    CARBON.showWarningDialog(org_wso2_carbon_hdfs_mgt_ui_jsi18n["resource.does.not.exit"] + " " + pickedPath.value + ".");
                    result = false;
                } else {
                    result = true;
                }
            },
            onFailure: function() {

            }
        });
	} else{
		CARBON.showWarningDialog(reason);
		result = false;
	}
	return result;
}

function validateResoucePath(){
	var pickedPath=$('pickedPath');
    return validateProvidedResoucePath(pickedPath, org_wso2_carbon_hdfs_mgt_ui_jsi18n["picked.path.empty"]);
}

YAHOO.util.Event.addListener(window, 'resize', truncateResourceNames);
YAHOO.util.Event.addListener(window, 'load', loadData);
var oldNodes = new Array();
var firstRun = true;
function truncateResourceNames(){
    if(document.getElementById('pointA') == null || $("entryList") == null){
		return;
	}
    var allNodes = YAHOO.util.Dom.getElementsByClassName("trimer");

	var wpWidth = YAHOO.util.Dom.getViewportWidth();
	var textAreaSize;
	if($('pointA') != null && $('pointA').style.display != "none"){
		textAreaSize = wpWidth - 770;
	}else{
		textAreaSize = wpWidth - 1030;
	}
	var textSize = (textAreaSize-(textAreaSize%6))/6;
    if(textSize <14){
        textSize = 14;
    }
    if(firstRun){
        oldNodes = new Array();
    }
	for (var i = 0; i < allNodes.length; i++) {
        var toTrim="";
        if(firstRun){
            oldNodes.push(allNodes[i].innerHTML);
            toTrim="" + allNodes[i].innerHTML;
        }else{
            toTrim="" + oldNodes[i];
        }

		if(toTrim.length>15) {
			allNodes[i].innerHTML = toTrim.truncate(textSize,'..');//toTrim.length+ " -- " +textSize + "/n";
		}

	}
    firstRun = false;
}
function loadData(){
    firstRun = true;
    truncateResourceNames();
}

function displaySuccessMessage(pickedPath, operation) {
    var message = "";
    var differentiate = "differentiate";
    new Ajax.Request('../resources/resource_exists_ajaxprocessor.jsp',
    {
        method:'post',
        parameters: {pickedPath: pickedPath, differentiate: differentiate,random:getRandom()},
        asynchronous:false,
        onSuccess: function(transport) {
            var returnValue = transport.responseText;
            if (returnValue.search(/----ResourceExists----/) != -1){
                message = org_wso2_carbon_hdfs_mgt_ui_jsi18n["successfully"] + " " + operation + " " + org_wso2_carbon_hdfs_mgt_ui_jsi18n["resource"] + ".";
            } else if (returnValue.search(/----CollectionExists----/) != -1){
                message = org_wso2_carbon_hdfs_mgt_ui_jsi18n["successfully"] + " " + operation + " " + org_wso2_carbon_hdfs_mgt_ui_jsi18n["collection"] + ".";
            }
        },
        onFailure: function() {

        }
    });
    CARBON.showInfoDialog(message);
}

function handleWindowOk(textBoxId, onOK){
     handleRelativeWindowOk("", textBoxId, onOK);
 }

function handleRelativeWindowOk(path, textBoxId, onOK) {
    if (!textBoxId) {
        return;
    }
    var theTextBox = document.getElementById(textBoxId);
    var pickedValue = document.getElementById('pickedPath').value;
    if (path != "") {
        pickedValue = pickedValue.replace(path, "");
    }
	if (textBoxId == "associationPaths") {
        // This is a hack to fix the ie7 issue with not getting the object using it's id
         document.forms['assoForm'].associationPaths.value = pickedValue;
    } else {
         theTextBox.value = pickedValue;
    }
    if (onOK && typeof onOK == "function") {
        onOK();
    }
}
function getRandom(){
    return Math.floor(Math.random() * 2000);
}

function toggleSaveMediaType() {
    if (jQuery('#toggleSaveMediaType_view').is(":visible")) {
        jQuery('#toggleSaveMediaType_view').hide();
        jQuery('#toggleSaveMediaType_edit').show();
        jQuery('#toggleSaveMediaType_editBtn').hide();
        jQuery('#toggleSaveMediaType_saveBtn').show();
        jQuery('#toggleSaveMediaType_cancelBtn').show();
    } else {
        jQuery('#toggleSaveMediaType_view').show();
        jQuery('#toggleSaveMediaType_edit').hide();
        jQuery('#toggleSaveMediaType_editBtn').show();
        jQuery('#toggleSaveMediaType_saveBtn').hide();
        jQuery('#toggleSaveMediaType_cancelBtn').hide();
    }
}

/**
 * Delete Row after deleting the item.
 * @param type
 * @param i
 */
function deleteRaw(type, i) {
    var propRow = document.getElementById(type + "Raw" + i);
    if (propRow != undefined && propRow != null) {
        var parentTBody = propRow.parentNode;
        if (parentTBody != undefined && parentTBody != null) {
            parentTBody.removeChild(propRow);
            if (!hasChildElements(parentTBody)) {
                var parentTable = document.getElementById(type + "Table");
                parentTable.style.display = 'none';
                var clButtonRaw = document.getElementById("clButtonRaw");
                if (clButtonRaw != undefined && clButtonRaw != null) {
                    clButtonRaw.style.display = 'none';
                }
            }
        }
    }
}

function navigatePages(wantedPage, resourcePath, viewMode, consumerID,
		targetDivID) {
	fillContentSection(resourcePath, wantedPage, viewMode, consumerID,
			targetDivID);
	YAHOO.util.Event.onAvailable("xx" + wantedPage, loadData);
}

function addRole(roleNamedivId) {
	sessionAwareFunction(function() {
		var userName = document.getElementById('user').value;
		var roleName = document.getElementById(roleNamedivId).value;
		var readAllowed = document.getElementById('readAllowed').value;
		var writeAllowed = document.getElementById('writeAllowed').value;
		var executeAllowed = document.getElementById('executeAllowed').value;

		if(readAllowed == "ra"){
			readAllowed = true;
		}if(writeAllowed == "wa"){
			writeAllowed = true;
		}if(executeAllowed == "da"){
			executeAllowed = true;
		}
           var reason = "";
		       reason += validateString(roleNamedivId);
		   if (reason == "") {
	            reason += validateIllegal(roleNamedivId);
	        }
	        if (reason == "") {
	            reason += validateResourcePathAndLength(document.getElementById(roleNamedivId));
	        }

	        if (reason != "") {
	            CARBON.showWarningDialog(reason);
	            return false;
	        }else{

				new Ajax.Request('../hdfsmgt/permissions_ajaxprocessor.jsp',
				         {
					method:'post',
		            parameters: {userName:userName,roleName:roleName,readAllowed:readAllowed,writeAllowed:writeAllowed,executeAllowed:executeAllowed},

		             onSuccess: function() {
		             	 CARBON.showInfoDialog(org_wso2_carbon_hdfs_mgt_ui_jsi18n["add.role.successful"], function(){
		             		 window.location = '../hdfsmgt/hdfs_roles.jsp';
		             		 });

		             },

		             onFailure: function(transport) {
		             	   CARBON.showErrorDialog(trim(transport.responseText),function(){
		            	          return;
		             	      });
		             }
				          });
	        	}
			    },org_wso2_carbon_hdfs_mgt_ui_jsi18n["session.timed.out"]);
}

function handlePeerCheckbox(myID, peerID) {

    //Only one is checked always, so it is safe to make the peer false
    if (myID !=  peerID) {
        var peer = document.getElementById(peerID);
        peer.checked = false;
    }
}

function validateString(fld1name) {
    var stringValue = document.getElementsByName(fld1name)[0].value;
    var errorMessage = "";
    if (stringValue=="" || stringValue == '') {
    	errorMessage = "Please fill empty fields.";
    }
    return errorMessage;
}