2 * Copyright (C) 2001, 2002 The Mir-coders group
\r
4 * This file is part of Mir.
\r
6 * Mir is free software; you can redistribute it and/or modify
\r
7 * it under the terms of the GNU General Public License as published by
\r
8 * the Free Software Foundation; either version 2 of the License, or
\r
9 * (at your option) any later version.
\r
11 * Mir is distributed in the hope that it will be useful,
\r
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
14 * GNU General Public License for more details.
\r
16 * You should have received a copy of the GNU General Public License
\r
17 * along with Mir; if not, write to the Free Software
\r
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
\r
20 * In addition, as a special exception, The Mir-coders gives permission to link
\r
21 * the code of this program with the com.oreilly.servlet library, any library
\r
22 * licensed under the Apache Software License, The Sun (tm) Java Advanced
\r
23 * Imaging library (JAI), The Sun JIMI library (or with modified versions of
\r
24 * the above that use the same license as the above), and distribute linked
\r
25 * combinations including the two. You must obey the GNU General Public
\r
26 * License in all respects for all of the code used other than the above
\r
27 * mentioned libraries. If you modify this file, you may extend this exception
\r
28 * to your version of the file, but you are not obligated to do so. If you do
\r
29 * not wish to do so, delete this exception statement from your version.
\r
32 package mircoders.entity;
\r
34 import java.io.File;
\r
35 import java.io.IOException;
\r
36 import java.io.InputStream;
\r
37 import java.sql.Connection;
\r
38 import java.sql.PreparedStatement;
\r
39 import java.sql.ResultSet;
\r
40 import java.sql.SQLException;
\r
41 import java.sql.Statement;
\r
43 import org.postgresql.largeobject.BlobInputStream;
\r
44 import org.postgresql.largeobject.LargeObject;
\r
45 import org.postgresql.largeobject.LargeObjectManager;
\r
47 import mir.config.MirPropertiesConfiguration;
\r
48 import mir.misc.FileUtil;
\r
49 import mir.misc.WebdbImage;
\r
50 import mir.log.LoggerWrapper;
\r
51 import mir.storage.StorageObject;
\r
52 import mir.storage.StorageObjectFailure;
\r
55 * Diese Klasse enth?lt die Daten eines MetaObjekts
\r
57 * @author RK, mh, mir-coders
\r
58 * @version $Id: EntityImages.java,v 1.18 2003/03/08 05:50:42 zapata Exp $
\r
62 public class EntityImages extends EntityUploadedMedia
\r
65 public EntityImages()
\r
69 logger = new LoggerWrapper("Entity.UploadedMedia.Images");
\r
72 public EntityImages(StorageObject theStorage) {
\r
74 setStorage(theStorage);
\r
81 public InputStream getImage() throws StorageObjectFailure {
\r
82 logger.debug("EntityImages.getimage started");
\r
83 java.sql.Connection con=null;
\r
84 Statement stmt=null;
\r
86 InputStream img_in = null;
\r
88 con = theStorageObject.getPooledCon();
\r
89 con.setAutoCommit(false);
\r
90 LargeObjectManager lom;
\r
91 java.sql.Connection jCon;
\r
92 stmt = con.createStatement();
\r
93 ResultSet rs = theStorageObject.executeSql(stmt,
\r
94 "select image_data from images where id="+getId());
\r
95 jCon = ((com.codestudio.sql.PoolManConnectionHandle)con)
\r
96 .getNativeConnection();
\r
97 lom = ((org.postgresql.Connection)jCon).getLargeObjectAPI();
\r
100 LargeObject lob = lom.open(rs.getInt(1));
\r
101 in = (BlobInputStream)lob.getInputStream();
\r
102 img_in = new ImageInputStream(in, con, stmt);
\r
107 catch (Throwable t) {
\r
108 logger.error("EntityImages.getImage failed: " + t.toString());
\r
109 t.printStackTrace(logger.asPrintWriter(LoggerWrapper.DEBUG_MESSAGE));
\r
112 con.setAutoCommit(true);
\r
114 catch (Throwable e) {
\r
115 logger.error("EntityImages.getImage resetting transaction mode failed: " + e.toString());
\r
116 e.printStackTrace(logger.asPrintWriter(LoggerWrapper.DEBUG_MESSAGE));
\r
120 theStorageObject.freeConnection(con, stmt);
\r
122 catch (Throwable e) {
\r
123 logger.error("EntityImages.getImage freeing connection failed: " +e.toString());
\r
126 throwStorageObjectFailure(t, "EntityImages -- getImage failed: ");
\r
131 public void setImage(InputStream in, String type)
\r
132 throws StorageObjectFailure {
\r
135 Connection con = null;
\r
136 PreparedStatement pstmt = null;
\r
139 logger.debug("EntityImages.settimage :: making internal representation of image");
\r
141 File tempDir = new File(MirPropertiesConfiguration.instance().getString("TempDir"));
\r
142 f = File.createTempFile("mir", ".tmp", tempDir);
\r
143 FileUtil.write(f, in);
\r
144 WebdbImage webdbImage= new WebdbImage(f, type);
\r
145 logger.debug("EntityImages.settimage :: made internal representation of image");
\r
147 con = theStorageObject.getPooledCon();
\r
148 con.setAutoCommit(false);
\r
149 logger.debug("EntityImages.settimage :: trying to insert image");
\r
152 LargeObjectManager lom;
\r
153 java.sql.Connection jCon;
\r
154 jCon = ((com.codestudio.sql.PoolManConnectionHandle)con)
\r
155 .getNativeConnection();
\r
157 lom = ((org.postgresql.Connection) jCon).getLargeObjectAPI();
\r
159 int oidImage = lom.create();
\r
160 int oidIcon = lom.create();
\r
161 LargeObject lobImage = lom.open(oidImage);
\r
162 LargeObject lobIcon = lom.open(oidIcon);
\r
163 webdbImage.setImage(lobImage.getOutputStream());
\r
164 webdbImage.setIcon(lobIcon.getOutputStream());
\r
168 setValueForProperty("img_height", new Integer(webdbImage.getImageHeight()).toString());
\r
169 setValueForProperty("img_width", new Integer(webdbImage.getImageWidth()).toString());
\r
170 setValueForProperty("icon_height", new Integer(webdbImage.getIconHeight()).toString());
\r
171 setValueForProperty("icon_width", new Integer(webdbImage.getIconWidth()).toString());
\r
172 setValueForProperty("image_data", new Integer(oidImage).toString());
\r
173 setValueForProperty("icon_data", new Integer(oidIcon).toString());
\r
175 } catch (Exception e) {
\r
176 throwStorageObjectFailure(e, "settimage :: setImage gescheitert: ");
\r
180 con.setAutoCommit(true);
\r
181 // get rid of the temp. file
\r
183 } catch (SQLException e) {
\r
184 throwStorageObjectFailure(e,"Resetting transaction-mode failed");
\r
187 theStorageObject.freeConnection(con,pstmt);
\r
193 * Takes an OutputStream as an argument and reads in the data
\r
194 * from the DB and writes it to the OutputStream.
\r
196 * It will also take care of closing the OutputStream.
\r
198 public InputStream getIcon() throws StorageObjectFailure {
\r
199 Connection con=null;
\r
200 Statement stmt=null;
\r
201 BlobInputStream in=null;
\r
202 ImageInputStream img_in=null;
\r
205 con = theStorageObject.getPooledCon();
\r
206 con.setAutoCommit(false);
\r
207 LargeObjectManager lom;
\r
208 java.sql.Connection jCon;
\r
209 stmt = con.createStatement();
\r
210 ResultSet rs = theStorageObject.executeSql(stmt, "select icon_data from images where id="+getId());
\r
211 jCon = ((com.codestudio.sql.PoolManConnectionHandle)con)
\r
212 .getNativeConnection();
\r
213 lom = ((org.postgresql.Connection)jCon).getLargeObjectAPI();
\r
216 LargeObject lob = lom.open(rs.getInt(1));
\r
217 in = (BlobInputStream)lob.getInputStream();
\r
218 img_in = new ImageInputStream( in, con ,stmt);
\r
219 //img_data = rs.getBytes(1);
\r
224 catch (Throwable t) {
\r
225 logger.error("EntityImages.getIcon failed: "+t.toString());
\r
226 t.printStackTrace(logger.asPrintWriter(LoggerWrapper.DEBUG_MESSAGE));
\r
229 con.setAutoCommit(true);
\r
231 catch (SQLException e) {
\r
232 logger.error("EntityImages.getIcon resetting transaction mode failed: " + e.toString());
\r
233 e.printStackTrace(logger.asPrintWriter(LoggerWrapper.DEBUG_MESSAGE));
\r
236 theStorageObject.freeConnection(con, stmt);
\r
238 catch (Throwable e) {
\r
239 logger.error("EntityImages -- freeing connection failed: " + e.getMessage());
\r
242 throwStorageObjectFailure(t, "EntityImages -- getIcon failed:");
\r
249 * a small wrapper class that allows us to store the DB connection resources
\r
250 * that the BlobInputStream is using and free them upon closing of the stream
\r
252 private class ImageInputStream extends InputStream {
\r
258 public ImageInputStream(BlobInputStream in, Connection con,
\r
265 public void close () throws IOException {
\r
268 _con.setAutoCommit(true);
\r
269 theStorageObject.freeConnection(_con,_stmt);
\r
270 } catch (Exception e) {
\r
271 throw new IOException("close(): "+e.toString());
\r
275 public int read() throws IOException {
\r