2 * Copyright (C) 2001, 2002 The Mir-coders group
4 * This file is part of Mir.
6 * Mir is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * Mir is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with Mir; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 * In addition, as a special exception, The Mir-coders gives permission to link
21 * the code of this program with any library licensed under the Apache Software License,
22 * The Sun (tm) Java Advanced Imaging library (JAI), The Sun JIMI library
23 * (or with modified versions of the above that use the same license as the above),
24 * and distribute linked combinations including the two. You must obey the
25 * GNU General Public License in all respects for all of the code used other than
26 * the above mentioned libraries. If you modify this file, you may extend this
27 * exception to your version of the file, but you are not obligated to do so.
28 * If you do not wish to do so, delete this exception statement from your version.
31 package mircoders.entity;
34 import java.io.IOException;
35 import java.io.InputStream;
36 import java.sql.Connection;
37 import java.sql.PreparedStatement;
38 import java.sql.ResultSet;
39 import java.sql.SQLException;
40 import java.sql.Statement;
42 import org.postgresql.largeobject.BlobInputStream;
43 import org.postgresql.largeobject.LargeObject;
44 import org.postgresql.largeobject.LargeObjectManager;
46 import mir.config.MirPropertiesConfiguration;
47 import mir.log.LoggerWrapper;
48 import mir.misc.FileUtil;
49 import mir.storage.StorageObject;
50 import mir.storage.StorageObjectFailure;
51 import mircoders.media.ImageProcessor;
54 * Diese Klasse enth?lt die Daten eines MetaObjekts
56 * @author RK, mh, mir-coders
57 * @version $Id: EntityImages.java,v 1.21 2003/04/30 00:37:27 zapata Exp $
61 public class EntityImages extends EntityUploadedMedia
63 private int maxImageSize = configuration.getInt("Producer.Image.MaxSize");
64 private int maxIconSize = configuration.getInt("Producer.Image.MaxIconSize");
65 private float minDescaleRatio = configuration.getFloat("Producer.Image.MinDescalePercentage")/100;
66 private int minDescaleReduction = configuration.getInt("Producer.Image.MinDescaleReduction");
73 logger = new LoggerWrapper("Entity.UploadedMedia.Images");
76 public EntityImages(StorageObject theStorage) {
78 setStorage(theStorage);
85 public InputStream getImage() throws StorageObjectFailure {
86 logger.debug("EntityImages.getimage started");
87 java.sql.Connection con=null;
90 InputStream img_in = null;
92 con = theStorageObject.getPooledCon();
93 con.setAutoCommit(false);
94 LargeObjectManager lom;
95 java.sql.Connection jCon;
96 stmt = con.createStatement();
97 ResultSet rs = theStorageObject.executeSql(stmt,
98 "select image_data from images where id="+getId());
99 jCon = ((com.codestudio.sql.PoolManConnectionHandle)con)
100 .getNativeConnection();
101 lom = ((org.postgresql.Connection)jCon).getLargeObjectAPI();
104 LargeObject lob = lom.open(rs.getInt(1));
105 in = (BlobInputStream)lob.getInputStream();
106 img_in = new ImageInputStream(in, con, stmt);
111 catch (Throwable t) {
112 logger.error("EntityImages.getImage failed: " + t.toString());
113 t.printStackTrace(logger.asPrintWriter(LoggerWrapper.DEBUG_MESSAGE));
116 con.setAutoCommit(true);
118 catch (Throwable e) {
119 logger.error("EntityImages.getImage resetting transaction mode failed: " + e.toString());
120 e.printStackTrace(logger.asPrintWriter(LoggerWrapper.DEBUG_MESSAGE));
124 theStorageObject.freeConnection(con, stmt);
126 catch (Throwable e) {
127 logger.error("EntityImages.getImage freeing connection failed: " +e.toString());
130 throwStorageObjectFailure(t, "EntityImages -- getImage failed: ");
135 public void setImage(InputStream in, String type) throws StorageObjectFailure {
139 Connection con = null;
140 PreparedStatement pstmt = null;
143 logger.debug("EntityImages.settimage :: making internal representation of image");
145 f = File.createTempFile("mir", ".tmp",
146 new File(MirPropertiesConfiguration.instance().getString("TempDir")));
147 FileUtil.write(f, in);
148 ImageProcessor processor = new ImageProcessor(f);
150 con = theStorageObject.getPooledCon();
151 con.setAutoCommit(false);
152 LargeObjectManager lom;
153 java.sql.Connection connection;
154 connection = ((com.codestudio.sql.PoolManConnectionHandle)con).getNativeConnection();
156 lom = ((org.postgresql.Connection) connection).getLargeObjectAPI();
158 int oidImage = lom.create();
159 LargeObject lobImage = lom.open(oidImage);
160 processor.descaleImage(maxImageSize, minDescaleRatio, minDescaleReduction);
161 processor.writeScaledData(lobImage.getOutputStream(), type);
163 setValueForProperty("img_height", new Integer(processor.getScaledHeight()).toString());
164 setValueForProperty("img_width", new Integer(processor.getScaledWidth()).toString());
166 int oidIcon = lom.create();
167 LargeObject lobIcon = lom.open(oidIcon);
168 processor.descaleImage(maxIconSize, minDescaleRatio, minDescaleReduction);
169 processor.writeScaledData(lobIcon.getOutputStream(), type);
172 setValueForProperty("icon_height", new Integer(processor.getScaledHeight()).toString());
173 setValueForProperty("icon_width", new Integer(processor.getScaledWidth()).toString());
175 setValueForProperty("image_data", new Integer(oidImage).toString());
176 setValueForProperty("icon_data", new Integer(oidIcon).toString());
179 catch (Exception e) {
180 throwStorageObjectFailure(e, "settimage :: setImage gescheitert: ");
185 con.setAutoCommit(true);
186 // get rid of the temp. file
188 } catch (SQLException e) {
189 throwStorageObjectFailure(e,"Resetting transaction-mode failed");
192 theStorageObject.freeConnection(con,pstmt);
198 * Takes an OutputStream as an argument and reads in the data
199 * from the DB and writes it to the OutputStream.
201 * It will also take care of closing the OutputStream.
203 public InputStream getIcon() throws StorageObjectFailure {
206 BlobInputStream in=null;
207 ImageInputStream img_in=null;
210 con = theStorageObject.getPooledCon();
211 con.setAutoCommit(false);
212 LargeObjectManager lom;
213 java.sql.Connection jCon;
214 stmt = con.createStatement();
215 ResultSet rs = theStorageObject.executeSql(stmt, "select icon_data from images where id="+getId());
216 jCon = ((com.codestudio.sql.PoolManConnectionHandle)con)
217 .getNativeConnection();
218 lom = ((org.postgresql.Connection)jCon).getLargeObjectAPI();
221 LargeObject lob = lom.open(rs.getInt(1));
222 in = (BlobInputStream)lob.getInputStream();
223 img_in = new ImageInputStream( in, con ,stmt);
224 //img_data = rs.getBytes(1);
229 catch (Throwable t) {
230 logger.error("EntityImages.getIcon failed: "+t.toString());
231 t.printStackTrace(logger.asPrintWriter(LoggerWrapper.DEBUG_MESSAGE));
234 con.setAutoCommit(true);
236 catch (SQLException e) {
237 logger.error("EntityImages.getIcon resetting transaction mode failed: " + e.toString());
238 e.printStackTrace(logger.asPrintWriter(LoggerWrapper.DEBUG_MESSAGE));
241 theStorageObject.freeConnection(con, stmt);
243 catch (Throwable e) {
244 logger.error("EntityImages -- freeing connection failed: " + e.getMessage());
247 throwStorageObjectFailure(t, "EntityImages -- getIcon failed:");
254 * a small wrapper class that allows us to store the DB connection resources
255 * that the BlobInputStream is using and free them upon closing of the stream
257 private class ImageInputStream extends InputStream {
263 public ImageInputStream(BlobInputStream in, Connection con,
270 public void close () throws IOException {
273 _con.setAutoCommit(true);
274 theStorageObject.freeConnection(_con,_stmt);
275 } catch (Exception e) {
276 throw new IOException("close(): "+e.toString());
280 public int read() throws IOException {