Ejemplo n.º 1
0
  /**
   * Specify the name of the class to use as the CustomChange. This method instantiates the class
   * using {@link #getClassLoader()} or fallback methods and assigns it to {@link
   * #getCustomChange()}. {@link #setClassLoader(ClassLoader)} must be called before this method.
   * The passed class is constructed, but no parameters are set. They are set in {@link
   * liquibase.change.Change#generateStatements(liquibase.database.Database)}
   */
  public CustomChangeWrapper setClass(String className) throws CustomChangeException {
    if (classLoader == null) {
      throw new CustomChangeException("CustomChangeWrapper classLoader not set");
    }
    this.className = className;
    try {
      try {
        customChange = (CustomChange) Class.forName(className, true, classLoader).newInstance();
      } catch (ClassCastException e) { // fails in Ant in particular
        try {
          customChange =
              (CustomChange)
                  Thread.currentThread().getContextClassLoader().loadClass(className).newInstance();
        } catch (ClassNotFoundException e1) {
          customChange = (CustomChange) Class.forName(className).newInstance();
        }
      }
    } catch (Exception e) {
      throw new CustomChangeException(e);
    }

    return this;
  }
Ejemplo n.º 2
0
  @Override
  public void load(ParsedNode parsedNode, ResourceAccessor resourceAccessor)
      throws ParsedNodeException {
    ChangeMetaData metaData = ChangeFactory.getInstance().getChangeMetaData(this);
    this.setResourceAccessor(resourceAccessor);
    try {
      for (ChangeParameterMetaData param : metaData.getParameters().values()) {
        if (Collection.class.isAssignableFrom(param.getDataTypeClass())) {
          Class collectionType = (Class) param.getDataTypeClassParameters()[0];
          if (param.getDataTypeClassParameters().length == 1) {
            if (ColumnConfig.class.isAssignableFrom(collectionType)) {
              List<ParsedNode> columnNodes =
                  new ArrayList<ParsedNode>(parsedNode.getChildren(null, param.getParameterName()));
              columnNodes.addAll(parsedNode.getChildren(null, "column"));

              Object nodeValue = parsedNode.getValue();
              if (nodeValue instanceof ParsedNode) {
                columnNodes.add((ParsedNode) nodeValue);
              } else if (nodeValue instanceof Collection) {
                for (Object nodeValueChild : ((Collection) nodeValue)) {
                  if (nodeValueChild instanceof ParsedNode) {
                    columnNodes.add((ParsedNode) nodeValueChild);
                  }
                }
              }

              for (ParsedNode child : columnNodes) {
                if (child.getName().equals("column") || child.getName().equals("columns")) {
                  List<ParsedNode> columnChildren = child.getChildren(null, "column");
                  if (columnChildren != null && columnChildren.size() > 0) {
                    for (ParsedNode columnChild : columnChildren) {
                      ColumnConfig columnConfig = (ColumnConfig) collectionType.newInstance();
                      columnConfig.load(columnChild, resourceAccessor);
                      ((ChangeWithColumns) this).addColumn(columnConfig);
                    }
                  } else {
                    ColumnConfig columnConfig = (ColumnConfig) collectionType.newInstance();
                    columnConfig.load(child, resourceAccessor);
                    ((ChangeWithColumns) this).addColumn(columnConfig);
                  }
                }
              }
            } else if (LiquibaseSerializable.class.isAssignableFrom(collectionType)) {
              List<ParsedNode> childNodes =
                  new ArrayList<ParsedNode>(parsedNode.getChildren(null, param.getParameterName()));
              for (ParsedNode childNode : childNodes) {
                LiquibaseSerializable childObject =
                    (LiquibaseSerializable) collectionType.newInstance();
                childObject.load(childNode, resourceAccessor);

                ((Collection) param.getCurrentValue(this)).add(childObject);
              }
            }
          }
        } else if (LiquibaseSerializable.class.isAssignableFrom(param.getDataTypeClass())) {
          try {
            ParsedNode child = parsedNode.getChild(null, param.getParameterName());
            if (child != null) {
              LiquibaseSerializable serializableChild =
                  (LiquibaseSerializable) param.getDataTypeClass().newInstance();
              serializableChild.load(child, resourceAccessor);
              param.setValue(this, serializableChild);
            }
          } catch (InstantiationException e) {
            throw new UnexpectedLiquibaseException(e);
          } catch (IllegalAccessException e) {
            throw new UnexpectedLiquibaseException(e);
          }
        } else {
          Object childValue =
              parsedNode.getChildValue(null, param.getParameterName(), param.getDataTypeClass());
          if (childValue == null
              && param.getSerializationType() == SerializationType.DIRECT_VALUE) {
            childValue = parsedNode.getValue();
          }
          param.setValue(this, childValue);
        }
      }
    } catch (InstantiationException e) {
      throw new UnexpectedLiquibaseException(e);
    } catch (IllegalAccessException e) {
      throw new UnexpectedLiquibaseException(e);
    }
    customLoadLogic(parsedNode, resourceAccessor);
    try {
      this.finishInitialization();
    } catch (SetupException e) {
      throw new ParsedNodeException(e);
    }
  }