001    /*
002      * mesopotamia @mesopotamia.version@
003     * Multilingual parser and repository. 
004     * Copyright (C) 2005  Hammurapi Group
005     *
006     * This program is free software; you can redistribute it and/or
007     * modify it under the terms of the GNU Lesser General Public
008     * License as published by the Free Software Foundation; either
009     * version 2 of the License, or (at your option) any later version.
010     *
011     * This program is distributed in the hope that it will be useful,
012     * but WITHOUT ANY WARRANTY; without even the implied warranty of
013     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014     * Lesser General Public License for more details.
015     *
016     * You should have received a copy of the GNU Lesser General Public
017     * License along with this library; if not, write to the Free Software
018     * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
019     *
020     * URL: http://http://www.hammurapi.biz
021     * e-Mail: support@hammurapi.biz
022     */
023    package org.mesopotamia;
024    
025    
026    import java.io.ByteArrayInputStream;
027    import java.io.ByteArrayOutputStream;
028    import java.io.InputStream;
029    import java.sql.SQLException;
030    import java.util.logging.Logger;
031    import java.util.zip.GZIPInputStream;
032    import java.util.zip.GZIPOutputStream;
033    
034    import org.mesopotamia.sql.LoadLevel;
035    import org.mesopotamia.sql.LoadLevelImpl;
036    
037    import biz.hammurapi.convert.ConvertingService;
038    import biz.hammurapi.util.ExceptionSink;
039    import biz.hammurapi.util.StreamPumper;
040    
041    /**
042     * This loader stores zipped file content to the repository without any processing. 
043     * 
044     * Definition of the loader in database 
045    <PRE> INSERT INTO LOADER (
046         LANGUAGE,
047         LANGUAGE_VERSION,
048         LEVEL,
049         LOADER_CLASS,
050         DESCRIPTION,
051         REQUIRES_SOURCE,
052         REQUIRES_ENVIRONMENT
053     ) VALUES (
054            '<I>language name</I>',
055            '<I>language version</I>',
056            'content',
057            'org.mesopotamia.ContentLoader',
058            'Loads source content into database',
059            1,
060            0
061     );</PRE>
062     * @author Pavel
063     */
064    public class ContentLoader extends LoaderBase implements SourceLoader {
065            private static final Logger logger = Logger.getLogger(ContentLoader.class.getName());
066    
067            public ContentLoader(RepositoryLanguage repoLanguage, LoaderEntry xData) {
068                    super(repoLanguage, xData);
069            }
070    
071            public boolean load(final int scanId, final int sourceUnitId, Source source, Object environment) {
072                    final RepositoryFactory factory=repoLanguage.getFactory();
073                    try {
074                            final LoadLevelImpl lli = new LoadLevelImpl(true);
075                            lli.setLevelId(data.getId());
076                            lli.setSourceUnitId(sourceUnitId);
077                            try {
078                                    final boolean[] pumpResult = {true};
079                                    InputStream in = (InputStream) ConvertingService.convert(source.get(), InputStream.class);
080                                    ByteArrayOutputStream baos = new ByteArrayOutputStream();
081                                    GZIPOutputStream gzos = new GZIPOutputStream(baos);
082                                    new StreamPumper(
083                                                    in, 
084                                                    gzos, 
085                                                    new ExceptionSink() {
086    
087                                                            public void consume(Object source, Exception e) {
088                                                                    lli.setLoadFailed(true);
089                                                                    try {
090                                                                            lli.setMessageId(storeErrorMessage(scanId, sourceUnitId, e));
091                                                                    } catch (SQLException e1) {
092                                                                            factory.consume(this, e);
093                                                                            pumpResult[0] = false;
094                                                                    }
095                                                                    logger.warning("Cannot load source '"+source+"' to level "+data.getLevel()+": "+e);
096                                                                    pumpResult[0] = false;
097                                                            }
098                                                            
099                                                    },
100                                                    true).run();
101                                    gzos.close();
102                                    lli.setLoaderData(baos.toByteArray());
103                                    
104                                    return pumpResult[0];
105                            } catch (Exception e) {
106                                    lli.setLoadFailed(true);
107                                    lli.setMessageId(storeErrorMessage(scanId, sourceUnitId, e));
108                                    logger.warning("Cannot load source '"+source+"' to level "+data.getLevel()+": "+e);
109                                    return false;
110                            } finally {
111                                    factory.getEngine().insertLoadLevel(lli);
112                            }
113                    } catch (SQLException e) {
114                            factory.consume(this, e);
115                            return false;
116                    }
117            }
118            
119            /**
120             * Returns source unit data as input stream.
121             */
122            public Object getData(int sourceUnitId, Integer scanId) throws MesopotamiaException {
123                    RepositoryFactory factory=repoLanguage.getFactory();
124                    try {
125                            LoadLevel loadLevel = factory.getEngine().getSourceUnitLoadLevel(data.getId(), sourceUnitId);
126                            if (loadLevel==null) {
127                                    return null;
128                            }
129                            return new GZIPInputStream(new ByteArrayInputStream(loadLevel.getLoaderData()));
130                    } catch (Exception e) {
131                            throw new MesopotamiaException("Cannot read tokens: "+e, e);
132                    }
133            }
134    }