Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorEike Stepper2012-05-08 12:53:45 +0000
committerEike Stepper2012-05-08 12:53:45 +0000
commit529ec4fd1a206d9a254ffcfcc75fbc2bb9378f7d (patch)
treec4928470c160b1f9f4109854484e121f15f04aff
parent0a3169f9bacae6ec6ebeee30934b011eabfd5ae3 (diff)
parent953a7624cf8f6b7dd0fd714c1d8e5aba306c5d7b (diff)
downloadcdo-529ec4fd1a206d9a254ffcfcc75fbc2bb9378f7d.tar.gz
cdo-529ec4fd1a206d9a254ffcfcc75fbc2bb9378f7d.tar.xz
cdo-529ec4fd1a206d9a254ffcfcc75fbc2bb9378f7d.zip
Merge remote-tracking branch 'origin/master'
-rw-r--r--plugins/org.eclipse.emf.cdo.server.hibernate.teneo/src/org/eclipse/emf/cdo/server/hibernate/internal/teneo/bundle/HibernateCommandProvider.java164
-rw-r--r--plugins/org.eclipse.emf.cdo.server.hibernate.teneo/src/org/eclipse/emf/cdo/server/hibernate/internal/teneo/bundle/OM.java96
2 files changed, 216 insertions, 44 deletions
diff --git a/plugins/org.eclipse.emf.cdo.server.hibernate.teneo/src/org/eclipse/emf/cdo/server/hibernate/internal/teneo/bundle/HibernateCommandProvider.java b/plugins/org.eclipse.emf.cdo.server.hibernate.teneo/src/org/eclipse/emf/cdo/server/hibernate/internal/teneo/bundle/HibernateCommandProvider.java
new file mode 100644
index 0000000000..7111ae539b
--- /dev/null
+++ b/plugins/org.eclipse.emf.cdo.server.hibernate.teneo/src/org/eclipse/emf/cdo/server/hibernate/internal/teneo/bundle/HibernateCommandProvider.java
@@ -0,0 +1,164 @@
+/*
+ * Copyright (c) 2004 - 2012 Eike Stepper (Berlin, Germany) and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * Erdal Karaca - initial API and implementation
+ */
+package org.eclipse.emf.cdo.server.hibernate.internal.teneo.bundle;
+
+import org.eclipse.emf.cdo.server.hibernate.teneo.CDOMappingGenerator;
+import org.eclipse.emf.cdo.spi.server.InternalRepository;
+import org.eclipse.emf.cdo.spi.server.RepositoryFactory;
+
+import org.eclipse.net4j.util.container.IPluginContainer;
+import org.eclipse.net4j.util.io.IOUtil;
+
+import org.eclipse.emf.ecore.EPackage;
+
+import org.eclipse.osgi.framework.console.CommandInterpreter;
+import org.eclipse.osgi.framework.console.CommandProvider;
+
+import org.osgi.framework.BundleContext;
+
+import java.io.FileOutputStream;
+import java.io.OutputStream;
+import java.util.Properties;
+
+/**
+ * Provides a command to export the hbm file directly from the osgi prompt.
+ *
+ * See:
+ *
+ * https://bugs.eclipse.org/bugs/show_bug.cgi?id=378797
+ *
+ * Exports the hbm through the following osgi command:
+ * hibernate mapping repo1 /tmp/hbm.xml
+ *
+ * @author Erdal Karaca
+ */
+public class HibernateCommandProvider implements CommandProvider
+{
+ private static final String NEW_LINE = "\r\n"; //$NON-NLS-1$
+
+ private static final String INDENT = " "; //$NON-NLS-1$
+
+ public HibernateCommandProvider(BundleContext bundleContext)
+ {
+ bundleContext.registerService(CommandProvider.class.getName(), this, null);
+ }
+
+ public String getHelp()
+ {
+ StringBuffer buffer = new StringBuffer();
+ buffer.append("---CDO Hibernate commands---" + NEW_LINE);
+ buffer.append(INDENT + "hibernate mapping - export generated hibernate file" + NEW_LINE);
+ return buffer.toString();
+ }
+
+ public Object _hibernate(CommandInterpreter interpreter)
+ {
+ try
+ {
+ String cmd = interpreter.nextArgument();
+ if ("mapping".equals(cmd))
+ {
+ exportHbm(interpreter);
+ return null;
+ }
+
+ interpreter.println(getHelp());
+ }
+ catch (CommandException ex)
+ {
+ interpreter.println(ex.getMessage());
+ }
+ catch (Exception ex)
+ {
+ interpreter.printStackTrace(ex);
+ }
+
+ return null;
+ }
+
+ private void exportHbm(CommandInterpreter interpreter) throws Exception
+ {
+ String syntax = "Syntax: hibernate mapping <repository-name> <export-file>";
+ InternalRepository repository = getRepository(interpreter, syntax);
+ String exportFile = nextArgument(interpreter, syntax);
+ OutputStream out = null;
+
+ try
+ {
+ out = new FileOutputStream(exportFile);
+ Properties props = new Properties();
+ props.putAll(repository.getProperties());
+ final EPackage[] ePackages = repository.getPackageRegistry().values().toArray(new EPackage[0]);
+
+ final CDOMappingGenerator mappingGenerator = new CDOMappingGenerator();
+ final String mapping = mappingGenerator.generateMapping(ePackages, props);
+ out.write(mapping.getBytes());
+ interpreter.println("Hibernate mapping exported");
+ }
+ finally
+ {
+ IOUtil.close(out);
+ }
+ }
+
+ private String nextArgument(CommandInterpreter interpreter, String syntax)
+ {
+ String argument = interpreter.nextArgument();
+ if (argument == null && syntax != null)
+ {
+ throw new CommandException(syntax);
+ }
+
+ return argument;
+ }
+
+ private InternalRepository getRepository(CommandInterpreter interpreter, String syntax)
+ {
+ String repositoryName = nextArgument(interpreter, syntax);
+ InternalRepository repository = getRepository(repositoryName);
+ if (repository == null)
+ {
+ throw new CommandException("Repository not found: " + repositoryName);
+ }
+
+ return repository;
+ }
+
+ private InternalRepository getRepository(String name)
+ {
+ for (Object element : IPluginContainer.INSTANCE.getElements(RepositoryFactory.PRODUCT_GROUP))
+ {
+ if (element instanceof InternalRepository)
+ {
+ InternalRepository repository = (InternalRepository)element;
+ if (repository.getName().equals(name))
+ {
+ return repository;
+ }
+ }
+ }
+
+ return null;
+ }
+
+ /**
+ * @author Eike Stepper
+ */
+ private static final class CommandException extends RuntimeException
+ {
+ private static final long serialVersionUID = 1L;
+
+ public CommandException(String message)
+ {
+ super(message);
+ }
+ }
+}
diff --git a/plugins/org.eclipse.emf.cdo.server.hibernate.teneo/src/org/eclipse/emf/cdo/server/hibernate/internal/teneo/bundle/OM.java b/plugins/org.eclipse.emf.cdo.server.hibernate.teneo/src/org/eclipse/emf/cdo/server/hibernate/internal/teneo/bundle/OM.java
index 111f81fd23..bb4945a434 100644
--- a/plugins/org.eclipse.emf.cdo.server.hibernate.teneo/src/org/eclipse/emf/cdo/server/hibernate/internal/teneo/bundle/OM.java
+++ b/plugins/org.eclipse.emf.cdo.server.hibernate.teneo/src/org/eclipse/emf/cdo/server/hibernate/internal/teneo/bundle/OM.java
@@ -1,44 +1,52 @@
-/*
- * Copyright (c) 2004 - 2012 Eike Stepper (Berlin, Germany) and others.
- * All rights reserved. This program and the accompanying materials
- * are made available under the terms of the Eclipse Public License v1.0
- * which accompanies this distribution, and is available at
- * http://www.eclipse.org/legal/epl-v10.html
- *
- * Contributors:
- * Eike Stepper - initial API and implementation
- */
-package org.eclipse.emf.cdo.server.hibernate.internal.teneo.bundle;
-
-import org.eclipse.net4j.util.om.OMBundle;
-import org.eclipse.net4j.util.om.OMPlatform;
-import org.eclipse.net4j.util.om.OSGiActivator;
-import org.eclipse.net4j.util.om.log.OMLogger;
-import org.eclipse.net4j.util.om.trace.OMTracer;
-
-/**
- * The <em>Operations & Maintenance</em> class of this bundle.
- *
- * @author Eike Stepper
- */
-public abstract class OM
-{
- public static final String BUNDLE_ID = "org.eclipse.emf.cdo.server.hibernate.teneo"; //$NON-NLS-1$
-
- public static final OMBundle BUNDLE = OMPlatform.INSTANCE.bundle(BUNDLE_ID, OM.class);
-
- public static final OMTracer DEBUG = BUNDLE.tracer("debug"); //$NON-NLS-1$
-
- public static final OMLogger LOG = BUNDLE.logger();
-
- /**
- * @author Eike Stepper
- */
- public static final class Activator extends OSGiActivator
- {
- public Activator()
- {
- super(BUNDLE);
- }
- }
-}
+/*
+ * Copyright (c) 2004 - 2012 Eike Stepper (Berlin, Germany) and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * Eike Stepper - initial API and implementation
+ * Erdal Karaca - added HibernateCommandProvider
+ */
+package org.eclipse.emf.cdo.server.hibernate.internal.teneo.bundle;
+
+import org.eclipse.net4j.util.om.OMBundle;
+import org.eclipse.net4j.util.om.OMPlatform;
+import org.eclipse.net4j.util.om.OSGiActivator;
+import org.eclipse.net4j.util.om.log.OMLogger;
+import org.eclipse.net4j.util.om.trace.OMTracer;
+
+/**
+ * The <em>Operations & Maintenance</em> class of this bundle.
+ *
+ * @author Eike Stepper
+ */
+public abstract class OM
+{
+ public static final String BUNDLE_ID = "org.eclipse.emf.cdo.server.hibernate.teneo"; //$NON-NLS-1$
+
+ public static final OMBundle BUNDLE = OMPlatform.INSTANCE.bundle(BUNDLE_ID, OM.class);
+
+ public static final OMTracer DEBUG = BUNDLE.tracer("debug"); //$NON-NLS-1$
+
+ public static final OMLogger LOG = BUNDLE.logger();
+
+ /**
+ * @author Eike Stepper
+ */
+ public static final class Activator extends OSGiActivator
+ {
+ public Activator()
+ {
+ super(BUNDLE);
+ }
+
+ @Override
+ protected void doStart() throws Exception
+ {
+ new HibernateCommandProvider(bundleContext);
+ }
+ }
+
+}

Back to the top