Package org.apache.openjpa.lib.meta

Examples of org.apache.openjpa.lib.meta.ClassArgParser


        Collection classes;
        if (args.length == 0) {
            log.info(_loc.get("running-all-classes"));
            classes = repos.loadPersistentTypes(true, loader);
        } else {
            ClassArgParser cap = conf.getMetaDataRepositoryInstance().
                getMetaDataFactory().newClassArgParser();
            cap.setClassLoader(loader);
            classes = new HashSet();
            for (int i = 0; i < args.length; i++)
                classes.addAll(Arrays.asList(cap.parseTypes(args[i])));
        }
        if (flags.name != null && classes.size() > 1)
            throw new UserException(_loc.get("name-mult-args", classes));

        ApplicationIdTool tool;
View Full Code Here


     */
    private void filterPersistenceCapable(List<File> files, Options opts) {
        JDBCConfiguration conf = new JDBCConfigurationImpl();
        Configurations.populateConfiguration(conf, opts);
        MetaDataRepository repo = conf.newMetaDataRepositoryInstance();
        ClassArgParser cap = repo.getMetaDataFactory().newClassArgParser();

        Iterator<File> fileIt = files.iterator();
        while (fileIt.hasNext()) {
            File classPath = fileIt.next();

            Class[] classes = cap.parseTypes(classPath.getAbsolutePath());

            if (classes == null) {
                getLog().info("Found no classes for " + classPath.getAbsolutePath());
            } else {
                for (int i = 0; i < classes.length; i++) {
View Full Code Here

            tool.setFile(flags.file);
        if (flags.writer != null)
            tool.setWriter(flags.writer);

        Log log = conf.getLog(OpenJPAConfiguration.LOG_TOOL);
        ClassArgParser cap = conf.getMetaDataRepositoryInstance().
            getMetaDataFactory().newClassArgParser();
        cap.setClassLoader(loader);
        Class[] classes;
        for (int i = 0; i < args.length; i++) {
            classes = cap.parseTypes(args[i]);
            for (int j = 0; j < classes.length; j++) {
                log.info(_loc.get("tool-running", classes[j], flags.action));
                try {
                    tool.run(classes[j]);
                } catch (IllegalArgumentException iae) {
View Full Code Here

    /**
     * Parse persistent type names.
     */
    protected Set<String> parsePersistentTypeNames(ClassLoader loader)
        throws IOException {
        ClassArgParser cparser = newClassArgParser();
        String[] clss;
        Set<String> names = new HashSet<String>();
        if (files != null) {
            File file;
            for (Iterator itr = files.iterator(); itr.hasNext();) {
                file = (File) itr.next();
                if ((AccessController.doPrivileged(J2DoPrivHelper
                    .isDirectoryAction(file))).booleanValue()) {
                    if (log.isTraceEnabled())
                        log.trace(_loc.get("scanning-directory", file));
                    scan(new FileMetaDataIterator(file, newMetaDataFilter()),
                        cparser, names, true, file);
                } else if (file.getName().endsWith(".jar")) {
                    if (log.isTraceEnabled())
                        log.trace(_loc.get("scanning-jar", file));
                    try {
                        ZipFile zFile = AccessController
                            .doPrivileged(J2DoPrivHelper
                                .newZipFileAction(file));
                        scan(new ZipFileMetaDataIterator(zFile,
                            newMetaDataFilter()), cparser, names, true, file);
                    } catch (PrivilegedActionException pae) {
                        throw (IOException) pae.getException();
                    }
                } else {
                    if (log.isTraceEnabled())
                        log.trace(_loc.get("scanning-file", file));
                    clss = cparser.parseTypeNames(new FileMetaDataIterator
                        (file));
                    List<String> newNames = Arrays.asList(clss);
                    if (log.isTraceEnabled())
                        log.trace(_loc.get("scan-found-names", newNames, file));
                    names.addAll(newNames);
                    File f = AccessController
                        .doPrivileged(J2DoPrivHelper
                            .getAbsoluteFileAction(file));
                    try {
                        mapPersistentTypeNames(AccessController
                            .doPrivileged(J2DoPrivHelper.toURLAction(f)), clss);
                    } catch (PrivilegedActionException pae) {
                        throw (FileNotFoundException) pae.getException();
                    }
                }
            }
        }
        URL url;
        if (urls != null) {
            for (Iterator itr = urls.iterator(); itr.hasNext();) {
                url = (URL) itr.next();
                if ("file".equals(url.getProtocol())) {
                    File file = AccessController
                        .doPrivileged(J2DoPrivHelper
                            .getAbsoluteFileAction(new File(url.getFile())));
                    if (files != null && files.contains(file)) {
                        continue;
                    } else if ((AccessController
                        .doPrivileged(J2DoPrivHelper.isDirectoryAction(file)))
                        .booleanValue()) {
                        if (log.isTraceEnabled())
                            log.trace(_loc.get("scanning-directory", file));
                        scan(
                            new FileMetaDataIterator(file, newMetaDataFilter()),
                            cparser, names, true, file);
                        continue;
                    }
                }
                if ("vfs".equals(url.getProtocol())) {
                    if (log.isTraceEnabled()) {
                        log.trace(_loc.get("scanning-vfs-url", url));
                    }

                    final URLConnection conn = url.openConnection();
                    final Object vfsContent = conn.getContent();
                    final URL finalUrl = url;
                    File file = AccessController.doPrivileged(new PrivilegedAction<File>() {
                        @SuppressWarnings({ "rawtypes", "unchecked" })
                        public File run() {
                            try {
                                Class virtualFileClass = Class.forName("org.jboss.vfs.VirtualFile");
                                Method getPhysicalFile = virtualFileClass.getDeclaredMethod("getPhysicalFile");
                                return (File) getPhysicalFile.invoke(vfsContent);
                            } catch (Exception e) {
                                log.error(_loc.get("while-scanning-vfs-url", finalUrl), e);
                            }
                            return null;
                        }
                    });
                    if (file != null)
                        scan(new FileMetaDataIterator(file, newMetaDataFilter()), cparser, names, true, file);

                    continue;
                }
                if ("jar".equals(url.getProtocol())) {
                    if (url.getPath().endsWith("!/")) {
                        if (log.isTraceEnabled())
                            log.trace(_loc.get("scanning-jar-url", url));
                        scan(new ZipFileMetaDataIterator(url,
                            newMetaDataFilter()), cparser, names, true, url);
                    } else {
                        if (log.isTraceEnabled())
                            log.trace(_loc.get("scanning-jar-url", url));
                        scan(new JarFileURLMetaDataIterator(url,
                            newMetaDataFilter()), cparser, names, true, url);
                    }                  
                } else if (url.getPath().endsWith(".jar")) {
                    if (log.isTraceEnabled())
                        log.trace(_loc.get("scanning-jar-at-url", url));
                    try {
                        InputStream is = (InputStream)
                            AccessController.doPrivileged(
                                J2DoPrivHelper.openStreamAction(url));
                        scan(new ZipStreamMetaDataIterator(
                            new ZipInputStream(is),
                            newMetaDataFilter()), cparser, names, true, url);
                    } catch (PrivilegedActionException pae) {
                        throw (IOException) pae.getException();
                    }
                } else {
                    // Open an InputStream from the URL and sniff for a zip header.  If it is, then this is
                    // a URL with a jar-formated InputStream, as per the JPA specification.  Otherwise, fall back
                    // to URLMetaDataIterator.
                    BufferedInputStream is = null;
                   
                    try {
                        is = new BufferedInputStream((InputStream) AccessController.
                            doPrivileged(J2DoPrivHelper.openStreamAction(url)));
                    } catch (PrivilegedActionException pae) {
                        throw (IOException) pae.getException();
                    }
                   
                    // Check for zip header magic 0x50 0x4b 0x03 0x04
                    is.mark(0);
                    boolean zipHeaderMatch = is.read() == 0x50 && is.read() == 0x4b && is.read() == 0x03 &&
                        is.read() == 0x04;
                    is.reset();
                   
                    if (zipHeaderMatch) {
                        // The URL provides a Jar-formatted InputStream, consume it with ZipStreamMetaDataIterator
                        if (log.isTraceEnabled())
                            log.trace(_loc.get("scanning-jar-at-url", url));
                        scan(new ZipStreamMetaDataIterator(new ZipInputStream(is), newMetaDataFilter()),
                            cparser, names, true, url);
                    } else {
                        // Fall back to URLMetaDataIterator
                        if (log.isTraceEnabled())
                            log.trace(_loc.get("scanning-url", url));
                        clss = cparser.parseTypeNames(new URLMetaDataIterator(url));
                        List<String> newNames = Arrays.asList(clss);
                        if (log.isTraceEnabled())
                            log.trace(_loc.get("scan-found-names", newNames, url));
                        names.addAll(newNames);
                        mapPersistentTypeNames(url, clss);
                    }                   
                }
            }
        }
        if (rsrcs != null) {
            String rsrc;
            MetaDataIterator mitr;
            for (Iterator itr = rsrcs.iterator(); itr.hasNext();) {
                rsrc = (String) itr.next();
                if (rsrc.endsWith(".jar")) {
                    url = AccessController.doPrivileged(
                        J2DoPrivHelper.getResourceAction(loader, rsrc));
                    if (url != null) {
                        if (log.isTraceEnabled())
                            log.trace(_loc.get("scanning-jar-stream-url", url));
                        try {
                            InputStream is = (InputStream)
                                AccessController.doPrivileged(
                                    J2DoPrivHelper.openStreamAction(url));
                            scan(new ZipStreamMetaDataIterator
                                (new ZipInputStream(is),
                                newMetaDataFilter()), cparser, names, true,
                                url);
                        } catch (PrivilegedActionException pae) {
                            throw (IOException) pae.getException();
                        }
                    }
                } else {
                    if (log.isTraceEnabled())
                        log.trace(_loc.get("scanning-resource", rsrc));
                    mitr = new ResourceMetaDataIterator(rsrc, loader);
                    OpenJPAConfiguration conf = repos.getConfiguration();
                    Map peMap = null;
                    if (conf instanceof OpenJPAConfigurationImpl)
                        peMap = ((OpenJPAConfigurationImpl)conf).getPersistenceEnvironment();
                    URL puUrl = peMap == null ? null : (URL) peMap.get(PERSISTENCE_UNIT_ROOT_URL);
                    List<String> mappingFileNames =
                        peMap == null ? null : (List<String>) peMap.get(MAPPING_FILE_NAMES);
                    List<URL> jars = peMap == null ? null : (List<URL>)peMap.get(JAR_FILE_URLS);
                    String puUrlString = puUrl == null ? null : puUrl.toString();
                    if (log.isTraceEnabled())
                        log.trace(_loc.get("pu-root-url", puUrlString));

                    URL puORMUrl = null;
                    try {
                        if (puUrlString != null) {
                            String puORMUrlStr = puUrlString + (puUrlString.endsWith("/") ? "" : "/") + rsrc;
                            puORMUrl = AccessController.doPrivileged(J2DoPrivHelper.createURL(puORMUrlStr));
                        }
                    } catch (PrivilegedActionException e) {
                        throw new IOException("Error generating puORMUrlStr.", e.getCause());
                    }

                    List<URL> urls = new ArrayList<URL>(3);
                    while (mitr.hasNext()) {
                        url = (URL) mitr.next();
                        String urlString = url.toString();
                        if (log.isTraceEnabled())
                            log.trace(_loc.get("resource-url", urlString));
                        if (peMap != null) {
                          //OPENJPA-2102: decode the URL to remove such things a spaces (' ') encoded as '%20'
                            if (puUrlString != null && decode(urlString).indexOf(decode(puUrlString)) != -1) {
                                urls.add(url);
                            } else if (puORMUrl != null && puORMUrl.equals(url)) {
                                // Check URL equality to support encapsulating URL protocols
                                urls.add(url);
                            }
                            if (mappingFileNames != null && mappingFileNames.size() != 0) {
                                for (String mappingFileName : mappingFileNames) {
                                    if (log.isTraceEnabled())
                                        log.trace(_loc.get("mapping-file-name", mappingFileName));
                                    if (urlString.indexOf(mappingFileName) != -1)
                                        urls.add(url);
                                }
                            }

                            if (jars != null && jars.size() != 0) {
                                for (URL jarUrl : jars) {
                                    if (log.isTraceEnabled())
                                        log.trace(_loc.get("jar-file-url", jarUrl));
                                    if (urlString.indexOf(jarUrl.toString()) != -1)
                                        urls.add(url);
                                }
                            }
                        } else {
                            urls.add(url);
                        }
                    }
                    mitr.close();

                    for (Object obj : urls) {
                        url = (URL) obj;
                        clss = cparser.parseTypeNames(new URLMetaDataIterator
                            (url));
                        List<String> newNames = Arrays.asList(clss);
                        if (log.isTraceEnabled())
                            log.trace(_loc.get("scan-found-names", newNames,
                                    rsrc));
View Full Code Here

        return _def;
    }

    @Override
    public ClassArgParser newClassArgParser() {
        ClassArgParser parser = new ClassArgParser();
        parser.setMetaDataStructure("package", null, new String[]{
            "entity", "embeddable", "mapped-superclass" }, "class");
        return parser;
    }
View Full Code Here

            log.info(_loc.get("running-all-classes"));
            classes = conf.getMappingRepositoryInstance().
                loadPersistentTypes(true, loader);
        } else {
            classes = new HashSet<Class<?>>();
            ClassArgParser classParser = conf.getMetaDataRepositoryInstance().
                getMetaDataFactory().newClassArgParser();
            classParser.setClassLoader(loader);
            Class<?>[] parsed;
            for (int i = 0; i < args.length; i++) {
                parsed = classParser.parseTypes(args[i]);
                classes.addAll(Arrays.asList(parsed));
            }
        }

        Class<?>[] act = (Class[]) classes.toArray(new Class[classes.size()]);
View Full Code Here

        return _def;
    }

    @Override
    public ClassArgParser newClassArgParser() {
        ClassArgParser parser = new ClassArgParser();
        parser.setMetaDataStructure("package", null, new String[]{
            "entity", "embeddable", "mapped-superclass" }, "class");
        return parser;
    }
View Full Code Here

            if (classes == null) {
              log.warn(_loc.get("no-class-to-enhance"));
              return false;
            }
        } else {
            ClassArgParser cap = conf.getMetaDataRepositoryInstance().
                getMetaDataFactory().newClassArgParser();
            cap.setClassLoader(loader);
            classes = new HashSet();
            for (int i = 0; i < args.length; i++)
                classes.addAll(Arrays.asList(cap.parseTypes(args[i])));
        }

        Project project = new Project();
        BCClass bc;
        PCEnhancer enhancer;
View Full Code Here

     */
    @Override
    protected Set<String> parsePersistentTypeNames(ClassLoader loader)
            throws IOException {
       
        ClassArgParser cparser = newClassArgParser();
        String[] clss;
        Set<String> names = new HashSet<String>();
        if (files != null) {
            File file;
            for (Iterator itr = files.iterator(); itr.hasNext();) {
                file = (File) itr.next();
                if ((AccessController.doPrivileged(J2DoPrivHelper.isDirectoryAction(file))).booleanValue()) {
                    if (log.isTraceEnabled()) {
                        log.trace(_loc.get("scanning-directory", file));
                    }
                    scan(new FileMetaDataIterator(file, newMetaDataFilter()), cparser, names, true, file);
                } else if (file.getName().endsWith(".jar")) {
                    if (log.isTraceEnabled()) {
                        log.trace(_loc.get("scanning-jar", file));
                    }
                    try {
                        ZipFile zFile = AccessController.doPrivileged(J2DoPrivHelper.newZipFileAction(file));
                        scan(new ZipFileMetaDataIterator(zFile, newMetaDataFilter()), cparser, names, true, file);
                    } catch (PrivilegedActionException pae) {
                        throw (IOException) pae.getException();
                    }
                } else {
                    if (log.isTraceEnabled()) {
                        log.trace(_loc.get("scanning-file", file));
                    }
                    clss = cparser.parseTypeNames(new FileMetaDataIterator(file));
                    List<String> newNames = Arrays.asList(clss);
                    if (log.isTraceEnabled()) {
                        log.trace(_loc.get("scan-found-names", newNames, file));
                    }
                    names.addAll(newNames);
                    File f = AccessController.doPrivileged(J2DoPrivHelper.getAbsoluteFileAction(file));
                    try {
                        mapPersistentTypeNames(AccessController.doPrivileged(J2DoPrivHelper.toURLAction(f)), clss);
                    } catch (PrivilegedActionException pae) {
                        throw (FileNotFoundException) pae.getException();
                    }
                }
            }
        }
        URL url;
        if (urls != null) {
            for (Iterator itr = urls.iterator(); itr.hasNext();) {
                url = (URL) itr.next();
                if ("file".equals(url.getProtocol())) {
                    File file = AccessController.doPrivileged(J2DoPrivHelper.getAbsoluteFileAction(
                            new File(url.getFile())));
                    if (files != null && files.contains(file)) {
                        continue;
                    } else if ((AccessController.doPrivileged(J2DoPrivHelper.isDirectoryAction(file))).booleanValue()) {
                        if (log.isTraceEnabled()) {
                            log.trace(_loc.get("scanning-directory", file));
                        }
                        scan(new FileMetaDataIterator(file, newMetaDataFilter()), cparser, names, true, file);
                        continue;
                    }
                }
                // OPENJPA-2229 - begin
                if ("vfs".equals(url.getProtocol())) {
                    if (log.isTraceEnabled()) {
                        log.trace(_loc.get("scanning-vfs-url", url));
                    }

                    URLConnection conn = url.openConnection();
                    Object vfsContent = conn.getContent();
                    try {
                        Class virtualFileClass = Class.forName("org.jboss.vfs.VirtualFile");
                        Method getPhysicalFile = virtualFileClass.getDeclaredMethod("getPhysicalFile");
                        File file = (File) getPhysicalFile.invoke(vfsContent);
                        scan(new FileMetaDataIterator(file, newMetaDataFilter()), cparser, names, true, file);
                    } catch (Exception e) {
                        log.error(_loc.get("while-scanning-vfs-url", url), e);
                    }

                    continue;
                }
                // OPENJPA-2229 - end
                if ("jar".equals(url.getProtocol())) {
                    if (url.getPath().endsWith("!/")) {
                        if (log.isTraceEnabled()) {
                            log.trace(_loc.get("scanning-jar-url", url));
                        }
                        scan(new ZipFileMetaDataIterator(url, newMetaDataFilter()), cparser, names, true, url);
                    } else {
                        if (log.isTraceEnabled()) {
                            log.trace(_loc.get("scanning-jar-url", url));
                        }
                        scan(new JarFileURLMetaDataIterator(url, newMetaDataFilter()), cparser, names, true, url);
                    }
                } else if (url.getPath().endsWith(".jar")) {
                    if (log.isTraceEnabled()) {
                        log.trace(_loc.get("scanning-jar-at-url", url));
                    }
                    try {
                        InputStream is = (InputStream) AccessController.doPrivileged(
                                J2DoPrivHelper.openStreamAction(url));
                        scan(new ZipStreamMetaDataIterator(new ZipInputStream(is), newMetaDataFilter()),
                                cparser, names, true, url);
                    } catch (PrivilegedActionException pae) {
                        throw (IOException) pae.getException();
                    }
                } else {
                    if (log.isTraceEnabled()) {
                        log.trace(_loc.get("scanning-url", url));
                    }
                    clss = cparser.parseTypeNames(new URLMetaDataIterator(url));
                    List<String> newNames = Arrays.asList(clss);
                    if (log.isTraceEnabled()) {
                        log.trace(_loc.get("scan-found-names", newNames, url));
                    }
                    names.addAll(newNames);
                    mapPersistentTypeNames(url, clss);
                }
            }
        }
        if (rsrcs != null) {
            String rsrc;
            MetaDataIterator mitr;
            for (Iterator itr = rsrcs.iterator(); itr.hasNext();) {
                rsrc = (String) itr.next();
                if (rsrc.endsWith(".jar")) {
                    url = AccessController.doPrivileged(
                            J2DoPrivHelper.getResourceAction(loader, rsrc));
                    if (url != null) {
                        if (log.isTraceEnabled()) {
                            log.trace(_loc.get("scanning-jar-stream-url", url));
                        }
                        try {
                            InputStream is = (InputStream) AccessController.doPrivileged(
                                    J2DoPrivHelper.openStreamAction(url));
                            scan(new ZipStreamMetaDataIterator(new ZipInputStream(is), newMetaDataFilter()), cparser,
                                    names, true, url);
                        } catch (PrivilegedActionException pae) {
                            throw (IOException) pae.getException();
                        }
                    }
                } else {
                    if (log.isTraceEnabled()) {
                        log.trace(_loc.get("scanning-resource", rsrc));
                    }
                    mitr = new ResourceMetaDataIterator(rsrc, loader);
                    OpenJPAConfiguration conf = repos.getConfiguration();
                    Map peMap = null;
                    if (conf instanceof OpenJPAConfigurationImpl) {
                        peMap = ((OpenJPAConfigurationImpl) conf).getPersistenceEnvironment();
                    }
                    URL puUrl = peMap == null ? null : (URL) peMap.get(PERSISTENCE_UNIT_ROOT_URL);
                    List<String> mappingFileNames =
                            peMap == null ? null : (List<String>) peMap.get(MAPPING_FILE_NAMES);
                    List<URL> jars = peMap == null ? null : (List<URL>) peMap.get(JAR_FILE_URLS);
                    String puUrlString = puUrl == null ? null : puUrl.toString();
                    if (log.isTraceEnabled()) {
                        log.trace(_loc.get("pu-root-url", puUrlString));
                    }

                    List<URL> mitrUrls = new ArrayList<URL>(3);
                    while (mitr.hasNext()) {
                        url = (URL) mitr.next();
                        String urlString = url.toString();
                        if (log.isTraceEnabled()) {
                            log.trace(_loc.get("resource-url", urlString));
                        }
                        if (peMap != null) {
                            //OPENJPA-2102: decode the URL to remove such things a spaces (' ') encoded as '%20'
                            if (puUrlString != null && decode(urlString).indexOf(decode(puUrlString)) != -1) {
                                mitrUrls.add(url);
                            }
                            if (mappingFileNames != null && !mappingFileNames.isEmpty()) {
                                for (String mappingFileName : mappingFileNames) {
                                    if (log.isTraceEnabled()) {
                                        log.trace(_loc.get("mapping-file-name", mappingFileName));
                                    }
                                    if (urlString.indexOf(mappingFileName) != -1) {
                                        mitrUrls.add(url);
                                    }
                                }
                            }

                            if (jars != null && !jars.isEmpty()) {
                                for (URL jarUrl : jars) {
                                    if (log.isTraceEnabled()) {
                                        log.trace(_loc.get("jar-file-url", jarUrl));
                                    }
                                    if (urlString.indexOf(jarUrl.toString()) != -1) {
                                        mitrUrls.add(url);
                                    }
                                }
                            }
                        } else {
                            mitrUrls.add(url);
                        }
                    }
                    mitr.close();

                    for (Object obj : mitrUrls) {
                        url = (URL) obj;
                        clss = cparser.parseTypeNames(new URLMetaDataIterator(url));
                        List<String> newNames = Arrays.asList(clss);
                        if (log.isTraceEnabled()) {
                            log.trace(_loc.get("scan-found-names", newNames, rsrc));
                        }
                        names.addAll(newNames);
View Full Code Here

        ClassLoader loader) {
        return null;
    }

    public ClassArgParser newClassArgParser() {
        return new ClassArgParser();
    }
View Full Code Here

TOP

Related Classes of org.apache.openjpa.lib.meta.ClassArgParser

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.