[jboss-cvs] JBossAS SVN: r68057 - in trunk: hibernate-int/src/main/org/jboss/hibernate/jmx and 1 other directories.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Fri Dec 7 18:27:16 EST 2007


Author: alesj
Date: 2007-12-07 18:27:16 -0500 (Fri, 07 Dec 2007)
New Revision: 68057

Added:
   trunk/hibernate-int/src/main/org/jboss/hibernate/jmx/HibernateMappingVisitor.java
Modified:
   trunk/hibernate-int/build.xml
   trunk/hibernate-int/src/main/org/jboss/hibernate/jmx/Hibernate.java
   trunk/hibernate-int/src/main/org/jboss/hibernate/jmx/HibernateMBean.java
   trunk/testsuite/src/resources/hibernate/mappings/Role.hbm.xml
   trunk/testsuite/src/resources/hibernate/mappings/User.hbm.xml
Log:
Initial work on JBAS-4242.
Still failing one test - see JIRA's forum post for details.

Modified: trunk/hibernate-int/build.xml
===================================================================
--- trunk/hibernate-int/build.xml	2007-12-07 22:06:42 UTC (rev 68056)
+++ trunk/hibernate-int/build.xml	2007-12-07 23:27:16 UTC (rev 68057)
@@ -78,6 +78,7 @@
       <path refid="cglib.classpath"/>
       <path refid="jboss.microcontainer.classpath"/>
       <path refid="jboss.integration.classpath"/>
+      <path refid="jboss.jboss.vfs.classpath"/>
     </path>
 
     <!-- ======= -->

Modified: trunk/hibernate-int/src/main/org/jboss/hibernate/jmx/Hibernate.java
===================================================================
--- trunk/hibernate-int/src/main/org/jboss/hibernate/jmx/Hibernate.java	2007-12-07 22:06:42 UTC (rev 68056)
+++ trunk/hibernate-int/src/main/org/jboss/hibernate/jmx/Hibernate.java	2007-12-07 23:27:16 UTC (rev 68057)
@@ -6,14 +6,14 @@
  */
 package org.jboss.hibernate.jmx;
 
-import java.io.File;
+import java.io.IOException;
 import java.net.URL;
 import java.net.URLClassLoader;
-import java.util.Properties;
+import java.util.Date;
 import java.util.HashSet;
 import java.util.Iterator;
-import java.util.Date;
-import java.util.jar.JarFile;
+import java.util.Properties;
+import java.util.Set;
 import javax.management.Notification;
 import javax.management.ObjectName;
 import javax.naming.InitialContext;
@@ -22,24 +22,26 @@
 import org.hibernate.HibernateException;
 import org.hibernate.Interceptor;
 import org.hibernate.SessionFactory;
-import org.hibernate.jmx.StatisticsService;
 import org.hibernate.cfg.Configuration;
 import org.hibernate.cfg.Environment;
+import org.hibernate.jmx.StatisticsService;
 import org.hibernate.transaction.JBossTransactionManagerLookup;
 import org.hibernate.transaction.JTATransactionFactory;
-
-import org.jboss.hibernate.cache.DeployedTreeCacheProvider;
+import org.jboss.deployment.DeploymentException;
 import org.jboss.hibernate.ListenerInjector;
+import org.jboss.hibernate.cache.DeployedTreeCacheProvider;
 import org.jboss.logging.Logger;
-import org.jboss.util.naming.Util;
-import org.jboss.system.ServiceMBeanSupport;
-import org.jboss.deployment.DeploymentException;
-import org.jboss.deployment.DeploymentInfo;
 import org.jboss.mx.loading.RepositoryClassLoader;
+import org.jboss.system.ServiceMBeanSupport;
+import org.jboss.util.naming.Util;
+import org.jboss.virtual.VFS;
+import org.jboss.virtual.VirtualFile;
+import org.jboss.virtual.VirtualFileVisitor;
 
 /**
  * The {@link HibernateMBean} implementation.
  *
+ * @author <a href="mailto:ales.justin at jboss.org">Ales JUstin</a>
  * @author <a href="mailto:alex at jboss.org">Alexey Loubyansky</a>
  * @author <a href="mailto:gavin at hibernate.org">Gavin King</a>
  * @author <a href="mailto:steve at hibernate.org">Steve Ebersole</a>
@@ -48,7 +50,6 @@
  */
 public class Hibernate extends ServiceMBeanSupport implements HibernateMBean
 {
-
    private static final Logger log = Logger.getLogger( Hibernate.class );
 
    public static final String SESSION_FACTORY_CREATE = "hibernate.sessionfactory.create";
@@ -88,8 +89,7 @@
    private String listenerInjector;
    private URL harUrl;
    private boolean scanForMappingsEnabled = false;
-   private HashSet archiveClasspathUrls = new HashSet();
-   private HashSet directoryClasspathUrls = new HashSet();
+   private Set classpathUrls = new HashSet();
 
    // Internal state
    private boolean dirty = false;
@@ -110,25 +110,9 @@
          destroySessionFactory();
       }
 
-      harUrl = determineHarUrl();
+      log.trace( "start scanning for mappings" );
+      scanForMappings();
 
-      if ( harUrl != null )
-      {
-         log.trace( "starting in har deployment mode" );
-         // we are part of a har deployment...
-         if ( scanForMappingsEnabled )
-         {
-            log.trace( "scan for mappings was enabled" );
-            scanForMappings();
-         }
-      }
-      else
-      {
-         // we are not contained within a har deployment...
-         log.trace( "starting in non-har deployment mode" );
-         scanForMappings();
-      }
-
       buildSessionFactory();
    }
 
@@ -138,32 +122,9 @@
    public void stopService() throws Exception
    {
       destroySessionFactory();
-      archiveClasspathUrls.clear();
-      directoryClasspathUrls.clear();
+      classpathUrls.clear();
    }
 
-   private URL determineHarUrl() throws Exception
-   {
-      log.trace( "Attempting to determine HarUrl..." );
-      DeploymentInfo deploymentInfo = getDeploymentInfo();
-      if ( deploymentInfo == null )
-      {
-         log.warn( "Unable to locate deployment info [" + getServiceName() + "]" );
-         return null;
-      }
-
-      String urlStr = deploymentInfo.url.getFile();
-      log.trace( "checking our deployment unit [" + urlStr + "]" );
-      if ( urlStr.endsWith( ".har" ) || urlStr.endsWith( ".har/" ) )
-      {
-         return deploymentInfo.url;
-      }
-      else
-      {
-         return null;
-      }
-   }
-
    /**
     * Centralize the logic needed for starting/binding the SessionFactory.
     *
@@ -204,15 +165,15 @@
          // Handle stat-mbean creation/registration....
          if ( sessionFactory.getStatistics() != null && sessionFactory.getStatistics().isStatisticsEnabled() )
          {
-			String serviceName = getServiceName().toString();
-			if( serviceName.indexOf("type=service") != -1 )
-			{
-				serviceName = serviceName.replaceAll("type=service","type=stats");
-			}
-			else
-			{
-				serviceName = serviceName + ",type=stats";
-			}
+            String serviceName = getServiceName().toString();
+            if( serviceName.indexOf("type=service") != -1 )
+            {
+               serviceName = serviceName.replaceAll("type=service","type=stats");
+            }
+            else
+            {
+               serviceName = serviceName + ",type=stats";
+            }
             hibernateStatisticsServiceName = new ObjectName(  serviceName );
             StatisticsService hibernateStatisticsService = new StatisticsService();
             hibernateStatisticsService.setSessionFactory( sessionFactory );
@@ -275,36 +236,15 @@
       }
    }
 
-   private void handleMappings(Configuration cfg)
+   private void handleMappings(Configuration cfg) throws IOException
    {
-      if ( harUrl != null )
-      {
-         final File file = new File( harUrl.getFile() );
-         if ( file.isDirectory() )
-         {
-            cfg.addDirectory( file );
-         }
-         else
-         {
-            cfg.addJar( file );
-         }
-      }
-
-      Iterator itr = archiveClasspathUrls.iterator();
+      Iterator itr = classpathUrls.iterator();
       while ( itr.hasNext() )
       {
-         final File archive = ( File ) itr.next();
-         log.debug( "Passing archive [" + archive + "] to Hibernate Configration" );
-         cfg.addJar( archive );
+         final URL url = (URL)itr.next();
+         log.debug( "Passing input stream [" + url + "] to Hibernate Configration" );
+         cfg.addInputStream( url.openStream() );
       }
-
-      itr = directoryClasspathUrls.iterator();
-      while ( itr.hasNext() )
-      {
-         final File directory = ( File ) itr.next();
-         log.debug( "Passing directory [" + directory + "] to Hibernate Configration" );
-         cfg.addDirectory( directory );
-      }
    }
 
    /**
@@ -317,7 +257,7 @@
       // Won't this cause problems if start() is called from say the console?
       // a way around is to locate our DeploymentInfo and grab its ucl attribute
       // for use here.
-      URL[] urls = null;
+      URL[] urls;
       ClassLoader cl = Thread.currentThread().getContextClassLoader();
       if ( cl instanceof RepositoryClassLoader )
       {
@@ -333,57 +273,37 @@
       }
 
       // Search the urls for each of the classpath entries for any containing
-      // hibernate mapping files or archives
+      // hibernate mapping files
+      VirtualFileVisitor visitor = new HibernateMappingVisitor(classpathUrls);
+      // visit har url if set
+      if (harUrl != null)
+         visitURL(harUrl, visitor);
+      // visit CL urls
       for ( int i = 0; i < urls.length; i++ )
       {
-         final File entry = new File( urls[i].getFile() );
-         log.trace( "checking classpath entry [" + entry + "]" );
-         if ( !entry.exists() )
-         {
-            continue;
-         }
-
-         if ( !entry.isDirectory() )
-         {
-            // This entry is not a directory, meaning it is a file of
-            // some sort.  If it is an archive, we are interested in it...
-            if ( isArchive( entry ) )
-            {
-               log.trace( "classpath entry was an archive file..." );
-               archiveClasspathUrls.add( entry );
-            }
-            else
-            {
-               log.trace( "classpath entry was a non-archive file..." );
-            }
-         }
-         else
-         {
-            log.trace( "classpath entry was a directory..." );
-
-            // we have a directory, add it to the list of directory classpath urls
-            directoryClasspathUrls.add( entry );
-         }
+         final URL entry = urls[i];
+         visitURL(entry, visitor);
       }
+      log.trace("Found mappings: " + classpathUrls);
    }
 
    /**
-    * Simple helper method to determine whether a given File instance represents an archive which complies with the JAR
-    * specification.
+    * Visit url for possible mapping files.
     *
-    * @param file The file to test.
-    * @return True if the incoming file for certain represents an archive; false otherwise.
+    * @param entry the url to visit
+    * @param visitor the visitor
     */
-   private boolean isArchive(File file)
+   protected void visitURL(URL entry, VirtualFileVisitor visitor)
    {
+      log.trace( "checking classpath entry [" + entry + "]" );
       try
       {
-         new JarFile( file );
-         return true;
+         VirtualFile vf = VFS.getRoot(entry);
+         vf.visit(visitor);
       }
-      catch ( Throwable t )
+      catch (IOException e)
       {
-         return false;
+         log.debug("Ignoring URL entry: " + entry + ", exception: " + e);
       }
    }
 
@@ -972,21 +892,4 @@
    {
       this.scanForMappingsEnabled = scanForMappingsEnabled;
    }
-
-   /**
-    * Retrieve MBean's current deployment status.
-    *
-    * @return the current status.
-    */
-   public DeploymentInfo getDeploymentInfo()
-   {
-      try
-      {
-         return super.getDeploymentInfo();
-      }
-      catch ( Throwable t )
-      {
-         return null;
-      }
-   }
 }

Modified: trunk/hibernate-int/src/main/org/jboss/hibernate/jmx/HibernateMBean.java
===================================================================
--- trunk/hibernate-int/src/main/org/jboss/hibernate/jmx/HibernateMBean.java	2007-12-07 22:06:42 UTC (rev 68056)
+++ trunk/hibernate-int/src/main/org/jboss/hibernate/jmx/HibernateMBean.java	2007-12-07 23:27:16 UTC (rev 68057)
@@ -6,16 +6,14 @@
  */
 package org.jboss.hibernate.jmx;
 
-import org.jboss.system.ServiceMBean;
-import org.jboss.deployment.DeploymentInfo;
-
-import org.hibernate.SessionFactory;
-
-import javax.management.MBeanRegistration;
-import javax.management.ObjectName;
 import java.net.URL;
 import java.util.Date;
+import javax.management.MBeanRegistration;
+import javax.management.ObjectName;
 
+import org.hibernate.SessionFactory;
+import org.jboss.system.ServiceMBean;
+
 /**
  * Describes a Hibernate service MBean.  Configures a
  * {@link org.hibernate.SessionFactory} instance and exposes it through JNDI.
@@ -64,13 +62,6 @@
 	 */
 	public void setScanForMappingsEnabled(boolean scanForMappings);
 
-   /**
-    * Retrieve MBean's current deployment info.
-    *
-    * @return the deployment info.
-    */
-   public DeploymentInfo getDeploymentInfo();
-
 	/**
 	 * The JNDI namespace where the managed {@link org.hibernate.SessionFactory} is to be bound.
 	 *

Added: trunk/hibernate-int/src/main/org/jboss/hibernate/jmx/HibernateMappingVisitor.java
===================================================================
--- trunk/hibernate-int/src/main/org/jboss/hibernate/jmx/HibernateMappingVisitor.java	                        (rev 0)
+++ trunk/hibernate-int/src/main/org/jboss/hibernate/jmx/HibernateMappingVisitor.java	2007-12-07 23:27:16 UTC (rev 68057)
@@ -0,0 +1,89 @@
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2006, JBoss Inc., and individual contributors as indicated
+* by the @authors tag. See the copyright.txt in the distribution for a
+* full listing of individual contributors.
+*
+* This is free software; you can redistribute it and/or modify it
+* under the terms of the GNU Lesser General Public License as
+* published by the Free Software Foundation; either version 2.1 of
+* the License, or (at your option) any later version.
+*
+* This software is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+* Lesser General Public License for more details.
+*
+* You should have received a copy of the GNU Lesser General Public
+* License along with this software; if not, write to the Free
+* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+*/
+package org.jboss.hibernate.jmx;
+
+import java.net.URL;
+import java.util.Set;
+
+import org.jboss.virtual.VirtualFile;
+import org.jboss.virtual.VirtualFileVisitor;
+import org.jboss.virtual.VisitorAttributes;
+
+/**
+ * Mapping visitor, matching .hbm.xml files.
+ *
+ * @author <a href="mailto:ales.justin at jboss.com">Ales Justin</a>
+ */
+public class HibernateMappingVisitor implements VirtualFileVisitor
+{
+   private Set<URL> urls;
+
+   public HibernateMappingVisitor(Set<URL> urls)
+   {
+      if (urls == null)
+         throw new IllegalArgumentException("Null urls");
+      this.urls = urls;
+   }
+
+   private static class MappingVisitorAttributes extends VisitorAttributes
+   {
+      public MappingVisitorAttributes()
+      {
+         setIgnoreErrors(true);
+         setIncludeHidden(false);
+         setIncludeRoot(false);
+         setLeavesOnly(false);
+         setRecurseFilter(RECURSE_ALL);
+      }
+   }
+
+   private static final VisitorAttributes MAPPING_ATTRIBUTES = new MappingVisitorAttributes();
+
+   public VisitorAttributes getAttributes()
+   {
+      return MAPPING_ATTRIBUTES;
+   }
+
+   public void visit(VirtualFile vf)
+   {
+      try
+      {
+         if (isMapping(vf))
+            urls.add(vf.toURL());
+      }
+      catch (Exception e)
+      {
+         new RuntimeException("Visit failed: " + e);
+      }
+   }
+
+   /**
+    * Is virtual file a mapping file.
+    *
+    * @param vf the virtual file
+    * @return true if virtual file is mapping
+    */
+   protected boolean isMapping(VirtualFile vf)
+   {
+      return vf.getName().indexOf(".hbm.xml") > 0;
+   }
+}

Modified: trunk/testsuite/src/resources/hibernate/mappings/Role.hbm.xml
===================================================================
--- trunk/testsuite/src/resources/hibernate/mappings/Role.hbm.xml	2007-12-07 22:06:42 UTC (rev 68056)
+++ trunk/testsuite/src/resources/hibernate/mappings/Role.hbm.xml	2007-12-07 23:27:16 UTC (rev 68057)
@@ -1,7 +1,7 @@
 <?xml version="1.0"?>
 <!DOCTYPE hibernate-mapping PUBLIC
-	"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
-	"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
+	"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
+	"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
 
 <hibernate-mapping package="org.jboss.test.hibernate.model">
 

Modified: trunk/testsuite/src/resources/hibernate/mappings/User.hbm.xml
===================================================================
--- trunk/testsuite/src/resources/hibernate/mappings/User.hbm.xml	2007-12-07 22:06:42 UTC (rev 68056)
+++ trunk/testsuite/src/resources/hibernate/mappings/User.hbm.xml	2007-12-07 23:27:16 UTC (rev 68057)
@@ -1,7 +1,7 @@
 <?xml version="1.0"?>
 <!DOCTYPE hibernate-mapping PUBLIC
-	"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
-	"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
+	"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
+	"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
 
 <hibernate-mapping package="org.jboss.test.hibernate.model">
 




More information about the jboss-cvs-commits mailing list