Search in sources :

Example 1 with DirLoc

use of org.jaffa.persistence.engines.jdbcengine.configservice.initdomain.DirLoc in project jaffa-framework by jaffa-projects.

the class ConfigurationService method parseNew.

private ClassMetaData parseNew(String classname) {
    if (log.isDebugEnabled()) {
        log.debug("Locating the mapping file for the Persistent class " + classname);
    }
    String shortname = classname.substring(classname.lastIndexOf('.') + 1);
    for (Iterator itr = m_locations.iterator(); itr.hasNext(); ) {
        DirLoc dirLoc = (DirLoc) itr.next();
        String packageName = dirLoc.getDir();
        StringBuffer urlNameBuf = new StringBuffer(packageName);
        if (!packageName.endsWith("/") && !packageName.endsWith("\\")) {
            urlNameBuf.append('/');
        }
        urlNameBuf.append(shortname).append(MAPPING_FILE_PREFIX);
        String urlName = urlNameBuf.toString();
        if (log.isDebugEnabled()) {
            log.debug("Looking for the mapping file " + urlName);
        }
        URL url = null;
        try {
            url = URLHelper.newExtendedURL(urlName);
        } catch (MalformedURLException e) {
            if (log.isDebugEnabled()) {
                log.debug("Could not find the mapping file " + urlName + ". " + e.getMessage());
            }
        }
        if (url != null) {
            DefaultHandler handler = new MappingParser();
            InputStream stream = null;
            try {
                XMLReader reader = XMLReaderFactory.createXMLReader(PARSER_NAME);
                reader.setContentHandler(handler);
                reader.setEntityResolver(new DefaultEntityResolver());
                reader.setErrorHandler(new DefaultErrorHandler());
                stream = url.openStream();
                reader.parse(new InputSource(stream));
            } catch (Exception e) {
                if (log.isDebugEnabled()) {
                    log.debug("Error in parsing the mapping file " + urlName + ". Will try and look at another location", e);
                }
                // try another location !!!
                continue;
            } finally {
                try {
                    if (stream != null) {
                        stream.close();
                    }
                } catch (IOException e) {
                // do nothing
                }
            }
            ClassMetaData classMetaData = ((MappingParser) handler).getMetaData();
            if (classMetaData != null && classMetaData.getClassName().equals(classname)) {
                if (log.isDebugEnabled()) {
                    log.debug("Validating the ClassMetaData object for the mapping file " + urlName);
                }
                classMetaData.setXmlFileUrl(url);
                classMetaData.validate();
                synchronized (m_metaCache) {
                    // one final check before inserting
                    if (m_metaCache.containsKey(classname)) {
                        classMetaData = (ClassMetaData) m_metaCache.get(classname);
                    } else {
                        m_metaCache.put(classname, classMetaData);
                    }
                }
                return classMetaData;
            } else {
                if (log.isDebugEnabled()) {
                    log.debug("The classname in the mapping file " + urlName + ", does not match the required value " + classname + ". Will try and look at another location");
                }
            }
        }
    }
    String str = "Could not find/parse the mapping file for the class " + classname;
    log.error(str);
    throw new ConfigurationServiceRuntimeException(str);
}
Also used : MalformedURLException(java.net.MalformedURLException) InputSource(org.xml.sax.InputSource) InputStream(java.io.InputStream) IOException(java.io.IOException) URL(java.net.URL) MalformedURLException(java.net.MalformedURLException) ConfigurationServiceRuntimeException(org.jaffa.persistence.engines.jdbcengine.configservice.exceptions.ConfigurationServiceRuntimeException) IOException(java.io.IOException) InitFileNotFoundRuntimeException(org.jaffa.persistence.engines.jdbcengine.configservice.exceptions.InitFileNotFoundRuntimeException) DefaultHandler(org.xml.sax.helpers.DefaultHandler) ConfigurationServiceRuntimeException(org.jaffa.persistence.engines.jdbcengine.configservice.exceptions.ConfigurationServiceRuntimeException) DefaultEntityResolver(org.jaffa.util.DefaultEntityResolver) DirLoc(org.jaffa.persistence.engines.jdbcengine.configservice.initdomain.DirLoc) DefaultErrorHandler(org.jaffa.util.DefaultErrorHandler) XMLReader(org.xml.sax.XMLReader)

Aggregations

IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 MalformedURLException (java.net.MalformedURLException)1 URL (java.net.URL)1 ConfigurationServiceRuntimeException (org.jaffa.persistence.engines.jdbcengine.configservice.exceptions.ConfigurationServiceRuntimeException)1 InitFileNotFoundRuntimeException (org.jaffa.persistence.engines.jdbcengine.configservice.exceptions.InitFileNotFoundRuntimeException)1 DirLoc (org.jaffa.persistence.engines.jdbcengine.configservice.initdomain.DirLoc)1 DefaultEntityResolver (org.jaffa.util.DefaultEntityResolver)1 DefaultErrorHandler (org.jaffa.util.DefaultErrorHandler)1 InputSource (org.xml.sax.InputSource)1 XMLReader (org.xml.sax.XMLReader)1 DefaultHandler (org.xml.sax.helpers.DefaultHandler)1