[jboss-user] [EJB 3.0] - Re: Retrieve retrieve Entity Class mapping from jboss
florian79
do-not-reply at jboss.com
Tue Feb 13 05:19:53 EST 2007
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;
| }
|
View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=4015698#4015698
Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=4015698
More information about the jboss-user
mailing list