/** {@inheritDoc} */
    @Override
    protected Map<String, String> run(Void arg) {
      Map<String, String> res = new HashMap<>();

      try {
        IgniteBiTuple<Collection<String>, Collection<String>> addrs =
            IgniteUtils.resolveLocalAddresses(InetAddress.getByName("0.0.0.0"));

        assert (addrs.get1() != null);
        assert (addrs.get2() != null);

        Iterator<String> ipIt = addrs.get1().iterator();
        Iterator<String> hostIt = addrs.get2().iterator();

        while (ipIt.hasNext() && hostIt.hasNext()) {
          String ip = ipIt.next();

          String hostName = hostIt.next();

          if (hostName == null || hostName.trim().isEmpty()) {
            try {
              if (InetAddress.getByName(ip).isLoopbackAddress()) res.put(ip, "localhost");
            } catch (Exception ignore) {
              // no-op
            }
          } else if (!hostName.equals(ip)) res.put(ip, hostName);
        }
      } catch (Exception e) {
        throw new IgniteException("Failed to resolve host name", e);
      }

      return res;
    }
Ejemplo n.º 2
0
  /**
   * @param springCfgPath Spring configuration file path.
   * @return Grid configuration.
   * @throws Exception If failed.
   */
  protected static IgniteConfiguration loadConfiguration(String springCfgPath) throws Exception {
    URL url;

    try {
      url = new URL(springCfgPath);
    } catch (MalformedURLException e) {
      url = IgniteUtils.resolveIgniteUrl(springCfgPath);

      if (url == null)
        throw new IgniteCheckedException(
            "Spring XML configuration path is invalid: "
                + springCfgPath
                + ". Note that this path should be either absolute or a relative local file system path, "
                + "relative to META-INF in classpath or valid URL to IGNITE_HOME.",
            e);
    }

    GenericApplicationContext springCtx;

    try {
      springCtx = new GenericApplicationContext();

      new XmlBeanDefinitionReader(springCtx).loadBeanDefinitions(new UrlResource(url));

      springCtx.refresh();
    } catch (BeansException e) {
      throw new Exception(
          "Failed to instantiate Spring XML application context [springUrl="
              + url
              + ", err="
              + e.getMessage()
              + ']',
          e);
    }

    Map<String, IgniteConfiguration> cfgMap;

    try {
      cfgMap = springCtx.getBeansOfType(IgniteConfiguration.class);
    } catch (BeansException e) {
      throw new Exception(
          "Failed to instantiate bean [type="
              + IgniteConfiguration.class
              + ", err="
              + e.getMessage()
              + ']',
          e);
    }

    if (cfgMap == null || cfgMap.isEmpty())
      throw new Exception("Failed to find ignite configuration in: " + url);

    return cfgMap.values().iterator().next();
  }
  /** {@inheritDoc} */
  @Override
  public void start() throws IgniteCheckedException {
    if (marsh instanceof BinaryMarshaller) {
      BinaryMetadataHandler metaHnd =
          new BinaryMetadataHandler() {
            @Override
            public void addMeta(int typeId, BinaryType newMeta) throws BinaryObjectException {
              assert newMeta != null;
              assert newMeta instanceof BinaryTypeImpl;

              BinaryMetadata newMeta0 = ((BinaryTypeImpl) newMeta).metadata();

              if (metaDataCache == null) {
                BinaryMetadata oldMeta = metaBuf.get(typeId);
                BinaryMetadata mergedMeta = PortableUtils.mergeMetadata(oldMeta, newMeta0);

                if (oldMeta != mergedMeta) {
                  synchronized (this) {
                    mergedMeta = PortableUtils.mergeMetadata(oldMeta, newMeta0);

                    if (oldMeta != mergedMeta) metaBuf.put(typeId, mergedMeta);
                    else return;
                  }

                  if (metaDataCache == null) return;
                  else metaBuf.remove(typeId);
                } else return;
              }

              assert metaDataCache != null;

              CacheObjectBinaryProcessorImpl.this.addMeta(typeId, newMeta0.wrap(portableCtx));
            }

            @Override
            public BinaryType metadata(int typeId) throws BinaryObjectException {
              if (metaDataCache == null) U.awaitQuiet(startLatch);

              return CacheObjectBinaryProcessorImpl.this.metadata(typeId);
            }
          };

      BinaryMarshaller pMarh0 = (BinaryMarshaller) marsh;

      portableCtx = new PortableContext(metaHnd, ctx.config());

      IgniteUtils.invoke(
          BinaryMarshaller.class, pMarh0, "setPortableContext", portableCtx, ctx.config());

      portableMarsh = new GridPortableMarshaller(portableCtx);

      portables = new IgniteBinaryImpl(ctx, this);
    }
  }
Ejemplo n.º 4
0
  /**
   * Creates and registers {@link BinaryClassDescriptor} for the given user {@code class}.
   *
   * @param cls Class.
   * @return Class descriptor.
   */
  private BinaryClassDescriptor registerUserClassDescriptor(Class<?> cls, boolean deserialize) {
    boolean registered;

    String typeName = typeName(cls.getName());

    BinaryIdMapper idMapper = userTypeIdMapper(typeName);

    int typeId = idMapper.typeId(typeName);

    try {
      registered = marshCtx.registerClass(typeId, cls);
    } catch (IgniteCheckedException e) {
      throw new BinaryObjectException("Failed to register class.", e);
    }

    BinarySerializer serializer = serializerForClass(cls);

    String affFieldName = affinityFieldName(cls);

    BinaryClassDescriptor desc =
        new BinaryClassDescriptor(
            this,
            cls,
            true,
            typeId,
            typeName,
            affFieldName,
            idMapper,
            serializer,
            true,
            registered);

    if (!deserialize) {
      Collection<BinarySchema> schemas =
          desc.schema() != null ? Collections.singleton(desc.schema()) : null;

      metaHnd.addMeta(
          typeId,
          new BinaryMetadata(
                  typeId, typeName, desc.fieldsMeta(), affFieldName, schemas, desc.isEnum())
              .wrap(this));
    }

    // perform put() instead of putIfAbsent() because "registered" flag might have been changed or
    // class loader
    // might have reloaded described class.
    if (IgniteUtils.detectClassLoader(cls).equals(dfltLdr)) userTypes.put(typeId, desc);

    descByCls.put(cls, desc);

    mappers.putIfAbsent(typeId, idMapper);

    return desc;
  }
Ejemplo n.º 5
0
  /**
   * @param clsName Class name.
   * @param idMapper ID mapper.
   * @param serializer Serializer.
   * @param affKeyFieldName Affinity key field name.
   * @param isEnum If enum.
   * @throws BinaryObjectException In case of error.
   */
  @SuppressWarnings("ErrorNotRethrown")
  public void registerUserType(
      String clsName,
      BinaryIdMapper idMapper,
      @Nullable BinarySerializer serializer,
      @Nullable String affKeyFieldName,
      boolean isEnum)
      throws BinaryObjectException {
    assert idMapper != null;

    Class<?> cls = null;

    try {
      cls = Class.forName(clsName);
    } catch (ClassNotFoundException | NoClassDefFoundError ignored) {
      // No-op.
    }

    String typeName = typeName(clsName);

    int id = idMapper.typeId(typeName);

    // Workaround for IGNITE-1358
    if (predefinedTypes.get(id) != null)
      throw new BinaryObjectException("Duplicate type ID [clsName=" + clsName + ", id=" + id + ']');

    if (mappers.put(id, idMapper) != null)
      throw new BinaryObjectException("Duplicate type ID [clsName=" + clsName + ", id=" + id + ']');

    if (affKeyFieldName != null) {
      if (affKeyFieldNames.put(id, affKeyFieldName) != null)
        throw new BinaryObjectException(
            "Duplicate type ID [clsName=" + clsName + ", id=" + id + ']');
    }

    typeMappers.put(typeName, idMapper);

    Map<String, Integer> fieldsMeta = null;
    Collection<BinarySchema> schemas = null;

    if (cls != null) {
      if (serializer == null) {
        // At this point we must decide whether to rely on Java serialization mechanics or not.
        // If no serializer is provided, we examine the class and if it doesn't contain non-trivial
        // serialization logic we are safe to fallback to reflective binary serialization.
        if (canUseReflectiveSerializer(cls)) serializer = new BinaryReflectiveSerializer();
      }

      BinaryClassDescriptor desc =
          new BinaryClassDescriptor(
              this,
              cls,
              true,
              id,
              typeName,
              affKeyFieldName,
              idMapper,
              serializer,
              true,
              true /* registered */);

      fieldsMeta = desc.fieldsMeta();
      schemas = desc.schema() != null ? Collections.singleton(desc.schema()) : null;

      if (IgniteUtils.detectClassLoader(cls).equals(dfltLdr)) userTypes.put(id, desc);

      descByCls.put(cls, desc);
    }

    metaHnd.addMeta(
        id,
        new BinaryMetadata(id, typeName, fieldsMeta, affKeyFieldName, schemas, isEnum).wrap(this));
  }