This content has been marked as final.
Show 2 replies
-
1. Re: Retrieve retrieve Entity Class mapping from jboss
florian79 Feb 13, 2007 5:19 AM (in response to fsommavilla)I hava just a bad solution, but it would be greate to find something better:
public static Class[] getClassesFromPackage(final String strPackageName) throws ClassNotFoundException { ArrayList<Class> classes = new ArrayList<Class>(); // Get a File object for the package File directory = null; String[] files = null; try { ClassLoader cld = Thread.currentThread().getContextClassLoader(); if (cld == null) { throw new ClassNotFoundException("Can't get class loader."); } String path = strPackageName.replace('.', '/'); //String path = '/' + pckgname.replace('.', '/'); URL resource = cld.getResource(path); if (resource == null) { throw new ClassNotFoundException("No resource for " + path); } if(resource.getPath().contains("!")) { String[] arZipPath = resource.getPath().split("\\!"); if(arZipPath.length >= 1) { String strZipPath = arZipPath[0].substring(6);//cut up 'file:/' try { JarFile jarFile = new JarFile(new File(strZipPath)); List<String> lClassNames = new ArrayList<String>(); for ( Enumeration e = jarFile.entries(); e.hasMoreElements(); ) { JarEntry target = (JarEntry) e.nextElement(); String strTargetName = target.getName(); if(strTargetName.endsWith(".class") && strTargetName.startsWith(path + "/") && !target.isDirectory()) { String strClassName = strTargetName.substring(path.length()+1); if(!strClassName.contains("/")) lClassNames.add(strClassName); } } files = (String[])lClassNames.toArray(new String[lClassNames.size()]); } catch (IOException e) { throw new ClassNotFoundException(strPackageName + " (" + strZipPath + ") does not appear to be a valid (JAR-) package"); } } } else { directory = new File(resource.getFile()); if (directory.exists()) { // Get the list of the files contained in the package files = directory.list(); Arrays.sort(files); } else { throw new ClassNotFoundException(strPackageName + " does not appear to be a valid package"); } } } catch (NullPointerException x) { throw new ClassNotFoundException(strPackageName + " (" + directory + ") does not appear to be a valid package"); } for (int i = 0; i < files.length; i++) { // we are only interested in .class files if (files.endsWith(".class")) { // removes the .class extension try { classes.add(Class.forName(strPackageName + '.' + files.substring(0, files.length() - 6))); } catch (Exception e) {} } } Class[] classesA = new Class[classes.size()]; classes.toArray(classesA); return classesA; }
-
2. Re: Retrieve retrieve Entity Class mapping from jboss
florian79 Feb 16, 2007 9:51 AM (in response to fsommavilla)I created a statless Sessionbean as followed:
@Stateless public class BaseSession implements BaseSessionRemote, BaseSessionLocal { @Resource SessionContext context; @PersistenceContext(unitName="myName") private EntityManager entityManager; public Map<String,EntityPersister> getAllClassMetadata() { Map<String,EntityPersister> metadata = null; try { HibernateEntityManager hibernateEntityManager = (HibernateEntityManager)entityManager.getDelegate(); metadata = hibernateEntityManager.getSession().getSessionFactory().getAllClassMetadata(); } catch(ClassCastException e) { LoggerImpl.getInstance().log(Logger.iLOGG_ERROR, this, e); metadata = new HashMap<String,EntityPersister>(); } return metadata; } }
and then iterate over all maped entities:Map m = myPortalSession.getEntityManager().getAllClassMetadata(); Set<Map.Entry> s = m.entrySet(); for (Map.Entry e : s) { System.out.println(e.getKey() + "--->" + e.getValue()); }