use of liquibase.exception.UnexpectedLiquibaseException in project liquibase by liquibase.
the class LabelExpression method matches.
private boolean matches(String expression, Labels runtimeLabels) {
if (runtimeLabels.isEmpty()) {
return true;
}
if (expression.trim().equals(":TRUE")) {
return true;
}
if (expression.trim().equals(":FALSE")) {
return false;
}
while (expression.contains("(")) {
Pattern pattern = Pattern.compile("(.*?)\\(([^\\(\\)]*?)\\)(.*)");
Matcher matcher = pattern.matcher(expression);
if (!matcher.matches()) {
throw new UnexpectedLiquibaseException("Cannot parse label pattern " + expression);
}
String parenExpression = matcher.group(2);
parenExpression = ":" + String.valueOf(matches(parenExpression, runtimeLabels)).toUpperCase();
expression = matcher.group(1) + " " + parenExpression + " " + matcher.group(3);
}
String[] orSplit = expression.split("\\s+or\\s+");
if (orSplit.length > 1) {
for (String split : orSplit) {
if (matches(split, runtimeLabels)) {
return true;
}
}
return false;
}
String[] andSplit = expression.split("\\s+and\\s+");
if (andSplit.length > 1) {
for (String split : andSplit) {
if (!matches(split, runtimeLabels)) {
return false;
}
}
return true;
}
boolean notExpression = false;
if (expression.startsWith("!")) {
notExpression = true;
expression = expression.substring(1);
} else if (expression.toLowerCase().startsWith("not ")) {
notExpression = true;
expression = expression.substring(4);
}
if (expression.trim().equals(":TRUE")) {
return !notExpression;
}
if (expression.trim().equals(":FALSE")) {
return notExpression;
}
for (String label : runtimeLabels.getLabels()) {
if (label.equalsIgnoreCase(expression)) {
return !notExpression;
}
}
return notExpression;
}
use of liquibase.exception.UnexpectedLiquibaseException in project liquibase by liquibase.
the class CDILiquibase method onStartup.
@PostConstruct
public void onStartup() {
log.info("Booting Liquibase " + LiquibaseUtil.getBuildVersion());
String hostName;
try {
hostName = NetUtil.getLocalHostName();
} catch (Exception e) {
log.warning("Cannot find hostname: " + e.getMessage());
log.debug("", e);
return;
}
LiquibaseConfiguration liquibaseConfiguration = LiquibaseConfiguration.getInstance();
if (!liquibaseConfiguration.getConfiguration(GlobalConfiguration.class).getShouldRun()) {
log.info("Liquibase did not run on " + hostName + " because " + liquibaseConfiguration.describeValueLookupLogic(GlobalConfiguration.class, GlobalConfiguration.SHOULD_RUN) + " was set to false");
return;
}
initialized = true;
try {
performUpdate();
} catch (LiquibaseException e) {
throw new UnexpectedLiquibaseException(e);
}
}
use of liquibase.exception.UnexpectedLiquibaseException in project liquibase by liquibase.
the class YamlSnapshotSerializer method toMap.
// @Override
// public String serialize(LiquibaseSerializable object, boolean pretty) {
// if (object instanceof DatabaseObject) {
// if (alreadySerializingObject) {
// return ((DatabaseObject) object).getObjectTypeName()+"#"+((DatabaseObject) object).getSnapshotId();
// } else {
// alreadySerializingObject = true;
// String string = super.serialize(object, pretty);
// alreadySerializingObject = false;
// return string;
// }
// }
// return super.serialize(object, pretty);
// }
@Override
protected Object toMap(final LiquibaseSerializable object) {
if (object instanceof DatabaseObject) {
if (alreadySerializingObject) {
String snapshotId = ((DatabaseObject) object).getSnapshotId();
if (snapshotId == null) {
String name = ((DatabaseObject) object).getName();
Object table = ((DatabaseObject) object).getAttribute("table", Object.class);
if (table == null) {
table = ((DatabaseObject) object).getAttribute("relation", Object.class);
}
if (table != null) {
name = table.toString() + "." + name;
}
if (((DatabaseObject) object).getSchema() != null) {
name = ((DatabaseObject) object).getSchema().toString() + "." + name;
}
throw new UnexpectedLiquibaseException("Found a null snapshotId for " + StringUtils.lowerCaseFirst(object.getClass().getSimpleName()) + " " + name);
}
return ((DatabaseObject) object).getClass().getName() + "#" + snapshotId;
} else {
alreadySerializingObject = true;
Object map = super.toMap(object);
alreadySerializingObject = false;
return map;
}
}
if (object instanceof DatabaseObjectCollection) {
SortedMap<String, Object> returnMap = new TreeMap<String, Object>();
for (Map.Entry<Class<? extends DatabaseObject>, Set<? extends DatabaseObject>> entry : ((DatabaseObjectCollection) object).toMap().entrySet()) {
ArrayList value = new ArrayList(entry.getValue());
Collections.sort(value, new DatabaseObjectComparator());
returnMap.put(entry.getKey().getName(), value);
}
return returnMap;
}
return super.toMap(object);
}
use of liquibase.exception.UnexpectedLiquibaseException in project liquibase by liquibase.
the class ServiceLocator method setResourceAccessor.
public void setResourceAccessor(ResourceAccessor resourceAccessor) {
this.resourceAccessor = resourceAccessor;
this.classesBySuperclass = new HashMap<Class, List<Class>>();
this.classResolver.setClassLoaders(new HashSet<ClassLoader>(Arrays.asList(new ClassLoader[] { resourceAccessor.toClassLoader() })));
if (packagesToScan == null) {
packagesToScan = new ArrayList<String>();
String packagesToScanSystemProp = System.getProperty("liquibase.scan.packages");
if ((packagesToScanSystemProp != null) && ((packagesToScanSystemProp = StringUtils.trimToNull(packagesToScanSystemProp)) != null)) {
for (String value : packagesToScanSystemProp.split(",")) {
addPackageToScan(value);
}
} else {
Set<InputStream> manifests;
try {
manifests = resourceAccessor.getResourcesAsStream("META-INF/MANIFEST.MF");
if (manifests != null) {
for (InputStream is : manifests) {
Manifest manifest = new Manifest(is);
String attributes = StringUtils.trimToNull(manifest.getMainAttributes().getValue("Liquibase-Package"));
if (attributes != null) {
for (Object value : attributes.split(",")) {
addPackageToScan(value.toString());
}
}
is.close();
}
}
} catch (IOException e) {
throw new UnexpectedLiquibaseException(e);
}
if (packagesToScan.size() == 0) {
addPackageToScan("liquibase.change");
addPackageToScan("liquibase.changelog");
addPackageToScan("liquibase.database");
addPackageToScan("liquibase.parser");
addPackageToScan("liquibase.precondition");
addPackageToScan("liquibase.datatype");
addPackageToScan("liquibase.serializer");
addPackageToScan("liquibase.sqlgenerator");
addPackageToScan("liquibase.executor");
addPackageToScan("liquibase.snapshot");
addPackageToScan("liquibase.logging");
addPackageToScan("liquibase.diff");
addPackageToScan("liquibase.structure");
addPackageToScan("liquibase.structurecompare");
addPackageToScan("liquibase.lockservice");
addPackageToScan("liquibase.sdk.database");
addPackageToScan("liquibase.ext");
}
}
}
}
use of liquibase.exception.UnexpectedLiquibaseException in project liquibase by liquibase.
the class DatabaseSnapshot method clone.
public DatabaseSnapshot clone(DatabaseObject[] examples) {
try {
DatabaseSnapshot returnSnapshot = new RestoredDatabaseSnapshot(this.database);
for (DatabaseObject example : examples) {
DatabaseObject existingObject = this.get(example);
if (existingObject == null) {
continue;
}
if (example instanceof Schema) {
for (Class<? extends DatabaseObject> type : this.snapshotControl.getTypesToInclude()) {
for (DatabaseObject object : this.get(type)) {
if (object.getSchema() == null) {
if (object instanceof Catalog) {
if (DatabaseObjectComparatorFactory.getInstance().isSameObject(object, ((Schema) example).getCatalog(), null, database)) {
returnSnapshot.allFound.add(object);
}
} else {
returnSnapshot.allFound.add(object);
}
} else {
if (DatabaseObjectComparatorFactory.getInstance().isSameObject(object.getSchema(), example, null, database)) {
returnSnapshot.allFound.add(object);
} else {
if (object.getClass().getName().contains("Synonym") && !object.getAttribute("private", false)) {
Schema objectSchema = object.getAttribute("objectSchema", Schema.class);
if (DatabaseObjectComparatorFactory.getInstance().isSameObject(objectSchema, example, null, database)) {
returnSnapshot.allFound.add(object);
}
}
}
}
}
}
} else {
returnSnapshot.allFound.add(existingObject);
}
}
returnSnapshot.getMetadata().putAll(this.getMetadata());
return returnSnapshot;
} catch (Exception e) {
throw new UnexpectedLiquibaseException(e);
}
}
Aggregations