diff options
Diffstat (limited to 'das-java/branches/das-java-M2/rdb/src/main/java/org/apache/tuscany/das/rdb/util')
4 files changed, 272 insertions, 0 deletions
diff --git a/das-java/branches/das-java-M2/rdb/src/main/java/org/apache/tuscany/das/rdb/util/ConfigUtil.java b/das-java/branches/das-java-M2/rdb/src/main/java/org/apache/tuscany/das/rdb/util/ConfigUtil.java new file mode 100644 index 0000000000..e3ae306485 --- /dev/null +++ b/das-java/branches/das-java-M2/rdb/src/main/java/org/apache/tuscany/das/rdb/util/ConfigUtil.java @@ -0,0 +1,58 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.das.rdb.util; + +import java.io.IOException; +import java.io.InputStream; + +import org.apache.tuscany.das.rdb.config.Config; +import org.apache.tuscany.das.rdb.config.ConfigFactory; +import org.apache.tuscany.sdo.util.SDOUtil; + +import commonj.sdo.helper.XMLHelper; + +/** + * Config util provides config-related utilities such as loading a Config + * instance from an InputStream + * + */ +public final class ConfigUtil { + + private ConfigUtil() { + + } + + public static Config loadConfig(InputStream configStream) { + + if (configStream == null) { + throw new RuntimeException("Cannot load configuration from a null InputStream. " + + "Possibly caused by an incorrect config xml file name"); + } + + SDOUtil.registerStaticTypes(ConfigFactory.class); + XMLHelper helper = XMLHelper.INSTANCE; + + try { + return (Config) helper.load(configStream).getRootObject(); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + +} diff --git a/das-java/branches/das-java-M2/rdb/src/main/java/org/apache/tuscany/das/rdb/util/DataObjectUtil.java b/das-java/branches/das-java-M2/rdb/src/main/java/org/apache/tuscany/das/rdb/util/DataObjectUtil.java new file mode 100644 index 0000000000..b9649669e5 --- /dev/null +++ b/das-java/branches/das-java-M2/rdb/src/main/java/org/apache/tuscany/das/rdb/util/DataObjectUtil.java @@ -0,0 +1,79 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.das.rdb.util; + +import java.util.Iterator; +import java.util.List; + +import commonj.sdo.ChangeSummary; +import commonj.sdo.DataObject; +import commonj.sdo.Property; +import commonj.sdo.ChangeSummary.Setting; +import commonj.sdo.helper.DataFactory; + +/** + */ +public final class DataObjectUtil { + + private DataObjectUtil() { + + } + + // Utilities + public static DataObject getRestoredCopy(DataObject changedDO) { + DataObject changedCopy = getCopy(changedDO); + restoreAttributeValues(changedCopy, changedDO); + return changedCopy; + } + + public static DataObject getCopy(DataObject original) { + + DataObject copy = DataFactory.INSTANCE.create(original.getType()); + + // Fill in values + Iterator i = original.getType().getProperties().iterator(); + while (i.hasNext()) { + Property feature = (Property) i.next(); + copy.set(feature, original.get(feature)); + } + return copy; + } + + /** + * @param changedCopy + * @return + */ + private static void restoreAttributeValues(DataObject changedCopy, DataObject changedDO) { + + ChangeSummary changeSummary = changedDO.getDataGraph().getChangeSummary(); + List changes = changeSummary.getOldValues(changedDO); + if (changes == null) { + return; + } + + Iterator i = changes.iterator(); + while (i.hasNext()) { + Setting s = (Setting) i.next(); + if (s.getProperty().getType().isDataType()) { + changedCopy.set(s.getProperty(), s.getValue()); + } + } + } + +} diff --git a/das-java/branches/das-java-M2/rdb/src/main/java/org/apache/tuscany/das/rdb/util/LoggerFactory.java b/das-java/branches/das-java-M2/rdb/src/main/java/org/apache/tuscany/das/rdb/util/LoggerFactory.java new file mode 100644 index 0000000000..bcd4386415 --- /dev/null +++ b/das-java/branches/das-java-M2/rdb/src/main/java/org/apache/tuscany/das/rdb/util/LoggerFactory.java @@ -0,0 +1,79 @@ +/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.tuscany.das.rdb.util;
+
+/* Import necessary log4j API classes */
+import org.apache.log4j.Appender;
+import org.apache.log4j.ConsoleAppender;
+import org.apache.log4j.Layout;
+import org.apache.log4j.Level;
+import org.apache.log4j.Logger;
+import org.apache.log4j.PatternLayout;
+
+/**
+ * Helper class for logging using external log utility : log4j
+ * This will use log4j and to configure it, please add/update log4j.properties
+ *
+ * @author lresende
+ */
+
+public final class LoggerFactory {
+ public static final LoggerFactory INSTANCE = new LoggerFactory();
+
+ private static Layout defaultLayout;
+
+ private static Appender defaultAppender;
+
+ static {
+ synchronized (LoggerFactory.class) {
+ defaultLayout = new PatternLayout(LoggerLayout.layout());
+ defaultAppender = new ConsoleAppender(defaultLayout);
+ }
+ }
+
+ private LoggerFactory() {
+
+ }
+
+ public Logger getLogger(Class loggingClass) {
+ Logger logger = Logger.getLogger(loggingClass);
+ logger.setLevel(Level.OFF);
+ logger.addAppender(defaultAppender);
+
+ return logger;
+ }
+
+ public Logger getLogger(Class loggingClass, Level logLevel) {
+ Logger logger = Logger.getLogger(loggingClass);
+ logger.setLevel(logLevel);
+ logger.addAppender(defaultAppender);
+
+ return logger;
+ }
+
+ public static void main(String[] args) {
+
+ Logger log = LoggerFactory.INSTANCE.getLogger(LoggerFactory.class);
+ if (log.isDebugEnabled()) {
+ log.debug("something");
+ }
+
+ }
+}
diff --git a/das-java/branches/das-java-M2/rdb/src/main/java/org/apache/tuscany/das/rdb/util/LoggerLayout.java b/das-java/branches/das-java-M2/rdb/src/main/java/org/apache/tuscany/das/rdb/util/LoggerLayout.java new file mode 100644 index 0000000000..f34330be48 --- /dev/null +++ b/das-java/branches/das-java-M2/rdb/src/main/java/org/apache/tuscany/das/rdb/util/LoggerLayout.java @@ -0,0 +1,56 @@ +/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.tuscany.das.rdb.util;
+
+/**
+ * Log4J layout helper for Tuscany DAS
+ * @author lresende
+ */
+public final class LoggerLayout {
+
+ private LoggerLayout() {
+
+ }
+
+ public static String layout() {
+
+ /*
+ * Substitute symbols
+ * %c Logger, %c{2 } last 2 partial names
+ * %C Class name (full agony), %C{2 } last 2 partial names
+ * %d{dd MMM yyyy HH:MM:ss } Date, format see java.text.SimpleDateFormat
+ * %F File name
+ * %l Location (caution: compiler-option-dependently)
+ * %L Line number
+ * %m user-defined message
+ * %M Method name
+ * %p Level
+ * %r Milliseconds since program start
+ * %t Threadname
+ * %x, %X see Doku
+ * %% individual percentage sign
+ *
+ * Caution: %C, %F, %l, %L, %M slow down program run!
+ * Note, %n is newline
+ */
+
+ return "[DAS RDB] - %c{1}.%M (%L) : %m %n";
+ }
+
+}
|