Пример #1
0
 @Override
 public List<URI> getMavenRepoURIs() {
   assertValid();
   try {
     List<URI> uris = new ArrayList<URI>();
     if (exists(curator.get(), ZkPath.MAVEN_PROXY.getPath("download")) != null) {
       List<String> children = getChildren(curator.get(), ZkPath.MAVEN_PROXY.getPath("download"));
       if (children != null && !children.isEmpty()) {
         Collections.sort(children);
       }
       if (children != null) {
         for (String child : children) {
           String mavenRepo =
               getSubstitutedPath(
                   curator.get(), ZkPath.MAVEN_PROXY.getPath("download") + "/" + child);
           if (mavenRepo != null && !mavenRepo.endsWith("/")) {
             mavenRepo += "/";
           }
           uris.add(new URI(mavenRepo));
         }
       }
     }
     return uris;
   } catch (Exception e) {
     throw FabricException.launderThrowable(e);
   }
 }
Пример #2
0
 private static String toString(Properties properties) {
   StringWriter writer = new StringWriter();
   try {
     properties.store(writer, "");
   } catch (IOException e) {
     throw FabricException.launderThrowable(e);
   }
   return writer.toString();
 }
Пример #3
0
 private static Properties mergeProperties(String left, Properties right) {
   Properties p = new Properties();
   try {
     p.load(new StringReader(left));
   } catch (IOException e) {
     throw FabricException.launderThrowable(e);
   }
   return mergeProperties(p, right);
 }
Пример #4
0
 private void releaseLock() {
   try {
     if (lock.isAcquiredInThisProcess()) {
       lock.release();
     }
   } catch (Exception e) {
     throw FabricException.launderThrowable(e);
   }
 }
Пример #5
0
  @Override
  public CreateContainerMetadata[] createContainers(
      CreateContainerOptions options, CreationStateListener listener) {
    assertValid();
    try {
      final ContainerProvider provider = getProvider(options.getProviderType());
      if (provider == null) {
        throw new FabricException(
            "Unable to find a container provider supporting '" + options.getProviderType() + "'");
      }

      String originalName = options.getName();
      if (originalName == null || originalName.length() == 0) {
        throw new FabricException("A name must be specified when creating containers");
      }

      if (listener == null) {
        listener = new NullCreationStateListener();
      }

      ObjectMapper mapper = new ObjectMapper();
      mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
      Map optionsMap = mapper.readValue(mapper.writeValueAsString(options), Map.class);
      String versionId =
          options.getVersion() != null ? options.getVersion() : getDataStore().getDefaultVersion();
      Set<String> profileIds = options.getProfiles();
      if (profileIds == null || profileIds.isEmpty()) {
        profileIds = new LinkedHashSet<String>();
        profileIds.add("default");
      }
      optionsMap.put("version", versionId);
      optionsMap.put("profiles", profileIds);
      optionsMap.put("number", 0);

      final List<CreateContainerMetadata> metadatas =
          new CopyOnWriteArrayList<CreateContainerMetadata>();
      int orgNumber = options.getNumber();
      int number = Math.max(orgNumber, 1);
      final CountDownLatch latch = new CountDownLatch(number);
      for (int i = 1; i <= number; i++) {
        String containerName;
        if (orgNumber >= 1) {
          containerName = originalName + i;
        } else {
          containerName = originalName;
        }
        optionsMap.put("name", containerName);

        // Check if datastore configuration has been specified and fallback to current container
        // settings.
        if (!hasValidDataStoreProperties(optionsMap)) {
          optionsMap.put("dataStoreProperties", getDataStore().getDataStoreProperties());
        }
        Class cl =
            options
                .getClass()
                .getClassLoader()
                .loadClass(options.getClass().getName() + "$Builder");
        CreateContainerBasicOptions.Builder builder =
            (CreateContainerBasicOptions.Builder)
                mapper.readValue(mapper.writeValueAsString(optionsMap), cl);
        // We always want to pass the obfuscated version of the password to the container provider.
        builder =
            (CreateContainerBasicOptions.Builder)
                builder.zookeeperPassword(PasswordEncoder.encode(getZookeeperPassword()));
        final CreateContainerOptions containerOptions = builder.build();
        final CreationStateListener containerListener = listener;
        new Thread("Creating container " + containerName) {
          public void run() {
            try {
              getDataStore().createContainerConfig(containerOptions);
              CreateContainerMetadata metadata =
                  provider.create(containerOptions, containerListener);
              if (metadata.isSuccess()) {
                Container parent =
                    containerOptions.getParent() != null
                        ? getContainer(containerOptions.getParent())
                        : null;
                // An ensemble server can be created without an existing ensemble.
                // In this case container config will be created by the newly created container.
                // TODO: We need to make sure that this entries are somehow added even to ensemble
                // servers.
                if (!containerOptions.isEnsembleServer()) {
                  getDataStore().createContainerConfig(metadata);
                }
                ContainerImpl container =
                    new ContainerImpl(parent, metadata.getContainerName(), FabricServiceImpl.this);
                metadata.setContainer(container);
                LOGGER.info(
                    "The container "
                        + metadata.getContainerName()
                        + " has been successfully created");
              } else {
                LOGGER.info(
                    "The creation of the container " + metadata.getContainerName() + " has failed",
                    metadata.getFailure());
              }
              metadatas.add(metadata);
            } catch (Throwable t) {
              CreateContainerBasicMetadata metadata = new CreateContainerBasicMetadata();
              metadata.setCreateOptions(containerOptions);
              metadata.setFailure(t);
              metadatas.add(metadata);
              getDataStore().deleteContainer(containerOptions.getName());
            } finally {
              latch.countDown();
            }
          }
        }.start();
      }
      if (!latch.await(15, TimeUnit.MINUTES)) {
        throw new FabricException("Timeout waiting for container creation");
      }
      return metadatas.toArray(new CreateContainerMetadata[metadatas.size()]);
    } catch (Exception e) {
      LOGGER.error("Failed to create containers " + e, e);
      throw FabricException.launderThrowable(e);
    }
  }