Repository: tomee Updated Branches: refs/heads/master dfeca89c7 -> acf9fa5ec
TOMEE-1527 debug helper to work with resources Project: http://git-wip-us.apache.org/repos/asf/tomee/repo Commit: http://git-wip-us.apache.org/repos/asf/tomee/commit/acf9fa5e Tree: http://git-wip-us.apache.org/repos/asf/tomee/tree/acf9fa5e Diff: http://git-wip-us.apache.org/repos/asf/tomee/diff/acf9fa5e Branch: refs/heads/master Commit: acf9fa5ecf2a72db2641172a3833a71b7a795a9e Parents: dfeca89 Author: Romain Manni-Bucau <[email protected]> Authored: Tue Mar 17 17:49:29 2015 +0100 Committer: Romain Manni-Bucau <[email protected]> Committed: Tue Mar 17 17:49:29 2015 +0100 ---------------------------------------------------------------------- .../openejb/config/EffectiveTomEEXml.java | 113 +++++++++++++++++++ .../org/apache/openejb/util/ListSetters.java | 88 +++++++++++++++ .../org.apache.openejb.cli/effective-tomee | 18 +++ .../META-INF/org.apache.openejb.cli/setters | 18 +++ .../openejb/config/EffectiveTomEEXmlTest.java | 42 +++++++ .../apache/openejb/util/ListSettersTest.java | 45 ++++++++ .../test/resources/EffectiveTomEEXmlTest.xml | 23 ++++ 7 files changed, 347 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/tomee/blob/acf9fa5e/container/openejb-core/src/main/java/org/apache/openejb/config/EffectiveTomEEXml.java ---------------------------------------------------------------------- diff --git a/container/openejb-core/src/main/java/org/apache/openejb/config/EffectiveTomEEXml.java b/container/openejb-core/src/main/java/org/apache/openejb/config/EffectiveTomEEXml.java new file mode 100644 index 0000000..23c3f7f --- /dev/null +++ b/container/openejb-core/src/main/java/org/apache/openejb/config/EffectiveTomEEXml.java @@ -0,0 +1,113 @@ +/* + * 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.openejb.config; + +import org.apache.commons.cli.CommandLine; +import org.apache.commons.cli.HelpFormatter; +import org.apache.commons.cli.OptionBuilder; +import org.apache.commons.cli.Options; +import org.apache.commons.cli.ParseException; +import org.apache.commons.cli.PosixParser; +import org.apache.openejb.assembler.classic.ResourceInfo; +import org.apache.openejb.cli.SystemExitException; +import org.apache.openejb.config.sys.JaxbOpenejb; +import org.apache.openejb.config.sys.Openejb; +import org.apache.openejb.config.sys.Resource; + +import java.io.File; +import javax.xml.bind.Marshaller; + +import static java.util.Arrays.asList; + +// TODO: i18n or useless? + +/** + * Will dump properties once resolved. + */ +public class EffectiveTomEEXml { + public static void main(final String[] args) throws Exception { + final CommandLine line = parseCommand(args); + if (line == null) { + return; + } + + final Openejb openejb = JaxbOpenejb.readConfig(findXml(line).getCanonicalPath()); + final ConfigurationFactory configFact = new ConfigurationFactory(); + + for (final Resource r : openejb.getResource()) { + final ResourceInfo ri = configFact.configureService(r, ResourceInfo.class); + if (!ri.properties.containsKey("SkipImplicitAttributes")) { + ri.properties.put("SkipImplicitAttributes", "false"); + } + r.getProperties().clear(); + r.getProperties().putAll(ri.properties); + } + + // TODO: others + + final Marshaller marshaller = JaxbOpenejb.getContext(Openejb.class).createMarshaller(); + marshaller.setProperty("jaxb.formatted.output", true); + marshaller.marshal(openejb, System.out); + } + + private static CommandLine parseCommand(final String[] args) throws SystemExitException { + final Options options = new Options(); + options.addOption(OptionBuilder.hasArg(true).withLongOpt("path").withDescription("[openejb|tomee].xml path").create("p")); + + final CommandLine line; + try { + line = new PosixParser().parse(options, args); + } catch (final ParseException exp) { + help(options); + throw new SystemExitException(-1); + } + + if (line.hasOption("help")) { + help(options); + return null; + } + return line; + } + + private static File findXml(final CommandLine line) { + File xml = null; + if (line.hasOption("path")) { + xml = new File(line.getOptionValue("path")); + } else { + for(final String config : asList("tomee.xml", "openejb.xml")) { + xml = new File( // we shouldnt go to catalina.base, just a fallback + System.getProperty("openejb.base", System.getProperty("openejb.home", System.getProperty("catalina.base", "missing"))), + config); + if (xml.isFile()) { + break; + } + } + } + if (xml == null || !xml.isFile()) { + throw new IllegalArgumentException(xml + " doesnt exist"); + } + return xml; + } + + private static void help(final Options options) { + new HelpFormatter().printHelp("effective-tomee [options] <value>", "\n", options, "\n"); + } + + private EffectiveTomEEXml() { + // no-op + } +} http://git-wip-us.apache.org/repos/asf/tomee/blob/acf9fa5e/container/openejb-core/src/main/java/org/apache/openejb/util/ListSetters.java ---------------------------------------------------------------------- diff --git a/container/openejb-core/src/main/java/org/apache/openejb/util/ListSetters.java b/container/openejb-core/src/main/java/org/apache/openejb/util/ListSetters.java new file mode 100644 index 0000000..de63f17 --- /dev/null +++ b/container/openejb-core/src/main/java/org/apache/openejb/util/ListSetters.java @@ -0,0 +1,88 @@ +/* + * 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.openejb.util; + +import org.apache.commons.cli.CommandLine; +import org.apache.commons.cli.HelpFormatter; +import org.apache.commons.cli.OptionBuilder; +import org.apache.commons.cli.Options; +import org.apache.commons.cli.ParseException; +import org.apache.commons.cli.PosixParser; +import org.apache.openejb.cli.SystemExitException; + +import java.lang.reflect.Method; +import java.lang.reflect.Modifier; +import java.util.Collection; +import java.util.Collections; +import java.util.LinkedList; +import java.util.List; + +// this helps to know which setters/properties we can use for nor owned code (OracleXaDataSource is a perfect example) +public class ListSetters { + public static void main(final String[] args) throws Exception { + final Options options = new Options(); + options.addOption(OptionBuilder + .isRequired(true).hasArg(true) + .withLongOpt("class").withDescription("the class to introspect").create("c")); + + final CommandLine line; + try { + line = new PosixParser().parse(options, args); + } catch (final ParseException exp) { + help(options); + throw new SystemExitException(-1); + } + + if (line.hasOption("help")) { + help(options); + return; + } + + final String clazz = line.getOptionValue("class"); + final Collection<Class<?>> ancestors = Classes.ancestors(Thread.currentThread().getContextClassLoader().loadClass(clazz)); + final List<String> list = new LinkedList<>(); + for (final Class<?> c : ancestors) { + for (final Method m : c.getDeclaredMethods()) { + if (!Modifier.isPublic(m.getModifiers())) { + continue; + } + + if (!m.getName().startsWith("set")) { + continue; + } + + if (m.getGenericParameterTypes().length != 1) { + continue; + } + + list.add(m.getName().substring(3)); + } + } + Collections.sort(list); + for (final String s : list) { + System.out.println("- " + s); + } + } + + private static void help(final Options options) { + new HelpFormatter().printHelp("setters [options] <value>", "\n", options, "\n"); + } + + private ListSetters() { + // no-op + } +} http://git-wip-us.apache.org/repos/asf/tomee/blob/acf9fa5e/container/openejb-core/src/main/resources/META-INF/org.apache.openejb.cli/effective-tomee ---------------------------------------------------------------------- diff --git a/container/openejb-core/src/main/resources/META-INF/org.apache.openejb.cli/effective-tomee b/container/openejb-core/src/main/resources/META-INF/org.apache.openejb.cli/effective-tomee new file mode 100644 index 0000000..00cc635 --- /dev/null +++ b/container/openejb-core/src/main/resources/META-INF/org.apache.openejb.cli/effective-tomee @@ -0,0 +1,18 @@ +# 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. +# +main.class = org.apache.openejb.config.EffectiveTomEEXml +name = effective-tomee +description = Shows properties TomEE will use for resources http://git-wip-us.apache.org/repos/asf/tomee/blob/acf9fa5e/container/openejb-core/src/main/resources/META-INF/org.apache.openejb.cli/setters ---------------------------------------------------------------------- diff --git a/container/openejb-core/src/main/resources/META-INF/org.apache.openejb.cli/setters b/container/openejb-core/src/main/resources/META-INF/org.apache.openejb.cli/setters new file mode 100644 index 0000000..0384323 --- /dev/null +++ b/container/openejb-core/src/main/resources/META-INF/org.apache.openejb.cli/setters @@ -0,0 +1,18 @@ +# 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. +# +main.class = org.apache.openejb.util.ListSetters +name = setters +description = List property available for a class http://git-wip-us.apache.org/repos/asf/tomee/blob/acf9fa5e/container/openejb-core/src/test/java/org/apache/openejb/config/EffectiveTomEEXmlTest.java ---------------------------------------------------------------------- diff --git a/container/openejb-core/src/test/java/org/apache/openejb/config/EffectiveTomEEXmlTest.java b/container/openejb-core/src/test/java/org/apache/openejb/config/EffectiveTomEEXmlTest.java new file mode 100644 index 0000000..98972ed --- /dev/null +++ b/container/openejb-core/src/test/java/org/apache/openejb/config/EffectiveTomEEXmlTest.java @@ -0,0 +1,42 @@ +/* + * 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.openejb.config; + +import org.junit.Test; + +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.PrintStream; + +import static org.apache.openejb.loader.JarLocation.jarLocation; +import static org.junit.Assert.assertTrue; + +public class EffectiveTomEEXmlTest { + @Test + public void run() throws Exception { + final PrintStream ps = System.out; + final ByteArrayOutputStream out = new ByteArrayOutputStream(); + System.setOut(new PrintStream(out)); + try { + final File filePath = new File(jarLocation(EffectiveTomEEXmlTest.class), getClass().getSimpleName() + ".xml"); + EffectiveTomEEXml.main(new String[]{ "-p", filePath.getAbsolutePath() }); + } finally { + System.setOut(ps); + } + assertTrue(new String(out.toByteArray()).contains("JdbcDriver=org.hsqldb.jdbcDriver")); + } +} http://git-wip-us.apache.org/repos/asf/tomee/blob/acf9fa5e/container/openejb-core/src/test/java/org/apache/openejb/util/ListSettersTest.java ---------------------------------------------------------------------- diff --git a/container/openejb-core/src/test/java/org/apache/openejb/util/ListSettersTest.java b/container/openejb-core/src/test/java/org/apache/openejb/util/ListSettersTest.java new file mode 100644 index 0000000..811c064 --- /dev/null +++ b/container/openejb-core/src/test/java/org/apache/openejb/util/ListSettersTest.java @@ -0,0 +1,45 @@ +/* + * 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.openejb.util; + +import org.junit.Test; + +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; + +import static org.junit.Assert.assertTrue; + +public class ListSettersTest { + @Test + public void run() throws Exception { + final PrintStream ps = System.out; + final ByteArrayOutputStream out = new ByteArrayOutputStream(); + System.setOut(new PrintStream(out)); + try { + ListSetters.main(new String[]{"-c", Foo.class.getName()}); + } finally { + System.setOut(ps); + } + assertTrue(new String(out.toByteArray()).contains("Str")); + assertTrue(new String(out.toByteArray()).contains("Integer")); + } + + public static class Foo { + public void setStr(String s) {} + public void setInteger(int s) {} + } +} http://git-wip-us.apache.org/repos/asf/tomee/blob/acf9fa5e/container/openejb-core/src/test/resources/EffectiveTomEEXmlTest.xml ---------------------------------------------------------------------- diff --git a/container/openejb-core/src/test/resources/EffectiveTomEEXmlTest.xml b/container/openejb-core/src/test/resources/EffectiveTomEEXmlTest.xml new file mode 100644 index 0000000..e93612e --- /dev/null +++ b/container/openejb-core/src/test/resources/EffectiveTomEEXmlTest.xml @@ -0,0 +1,23 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + 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. +--> +<tomee> + <Resource id="debug" type="DataSource"> + Ignored = yes + JdbcUrl = jdbc:hsqldb:mem:foo + </Resource> +</tomee> \ No newline at end of file
