/** * Creates and returns a new Image object that is a rendering of this Image object. The input * object defines the rendering parameters of the new Image object. Imagemagick's convert program * is used to create a scaled bounding box of this Image with the given dimensions. * * @param {Object} input A JavaScript object specifying the rendering parameters. For example, * <code> {maxWidth:200, maxHeight:100} </code> * @returns {Image} The rendered Image object * @throws Exception */ public ImageObject jsFunction_render(Object input) throws Exception { if (input == null || !(input instanceof Scriptable)) { throw new RuntimeException("The first argument to render() must be a scriptable object."); } Scriptable s = (Scriptable) input; int maxWidth = toInt(s.get("maxWidth", s)); int maxHeight = toInt(s.get("maxHeight", s)); int cropWidth = toInt(s.get("cropWidth", s)); int cropHeight = toInt(s.get("cropHeight", s)); int cropXOffset = toInt(s.get("cropXOffset", s)); int cropYOffset = toInt(s.get("cropYOffset", s)); int currentWidth = (int) node.getInteger(WIDTH); int currentHeight = (int) node.getInteger(HEIGHT); String aname = null; if (maxWidth > 0 && maxHeight > 0 && currentWidth > 0 && currentHeight > 0) { int[] dims = this.computeResizedDimensions(maxWidth, maxHeight, currentWidth, currentHeight); aname = dims[0] + "x" + dims[1]; maxWidth = dims[0]; maxHeight = dims[1]; } else if (cropWidth > 0 && cropHeight > 0) { aname = cropWidth + "x" + cropHeight + "_" + cropXOffset + "x" + cropYOffset; } else { throw new RuntimeException("render(), invalid parameter set."); } Object o = this.jsFunction_get(aname); if (o instanceof ImageObject) { return (ImageObject) o; } try { synchronized (this) { while (this.convertOps.contains(aname)) { this.wait(); } this.convertOps.add(aname); } o = ((axiom.objectmodel.db.Node) this.node).getChildElement(aname, true); if (o instanceof Node) { return (ImageObject) Context.toObject(o, this.core.global); } ImageObject computedImg = null; String[] paths = getPaths(); String imgPath = paths[0]; String tmpPath = paths[1]; String fileName = node.getString(FileObject.FILE_NAME); try { File tmpFile = new File(tmpPath); int[] dims = null; if (maxWidth > 0 && maxHeight > 0) { dims = this.resize( maxWidth, maxHeight, (int) this.node.getInteger(WIDTH), (int) this.node.getInteger(HEIGHT), imgPath, tmpPath, true); } else { dims = this.crop(cropWidth, cropHeight, cropXOffset, cropYOffset, imgPath, tmpPath); } if (dims == null) { throw new Exception("ImageObject.render(), resizing the image failed."); } final String protoname = "Image"; INode node = new axiom.objectmodel.db.Node(protoname, protoname, core.app.getWrappedNodeManager()); computedImg = new ImageObject("Image", core, node, core.getPrototype(protoname), true); node.setString(FileObject.FILE_NAME, fileName); node.setString(FileObject.ACCESSNAME, FileObjectCtor.generateAccessName(fileName)); node.setString(FileObject.CONTENT_TYPE, this.node.getString(FileObject.CONTENT_TYPE)); node.setJavaObject(FileObject.SELF, computedImg); node.setInteger(ImageObject.WIDTH, dims[0]); node.setInteger(ImageObject.HEIGHT, dims[1]); node.setString(FileObject.RENDERED_CONTENT, "true"); node.setInteger(FileObject.FILE_SIZE, tmpFile.length()); computedImg.tmpPath = tmpPath; } catch (Exception ex) { throw new RuntimeException( "ImageObject.jsfunction_bound(): Could not write the image to temporary storage, " + ex.getMessage()); } if (computedImg != null) { this.jsFunction_addThumbnail(computedImg, null); return computedImg; } } finally { synchronized (this) { this.convertOps.remove(aname); this.notifyAll(); } } return null; }
public void convert(Application app, File dbhome) throws Exception { FSDirectory indexDir = FSDirectory.getDirectory(dbhome, false); if (indexDir instanceof TransFSDirectory) { FSDirectory.setDisableLocks(true); TransFSDirectory d = (TransFSDirectory) indexDir; TransSource source = app.getTransSource(); d.setDriverClass(source.getDriverClass()); d.setUrl(source.getUrl()); d.setUser(source.getUser()); d.setPassword(source.getPassword()); } File ndbhome = new File(dbhome.getParentFile(), dbhome.getName() + "_tmp"); File olddbhome = new File(dbhome.getParentFile(), dbhome.getName() + "_old"); FSDirectory nindexDir = FSDirectory.getDirectory(ndbhome, true); if (nindexDir instanceof TransFSDirectory) { FSDirectory.setDisableLocks(true); TransFSDirectory d = (TransFSDirectory) nindexDir; TransSource source = app.getTransSource(); d.setDriverClass(source.getDriverClass()); d.setUrl(source.getUrl()); d.setUser(source.getUser()); d.setPassword(source.getPassword()); } IndexSearcher searcher = null; IndexWriter writer = null; LuceneManager lmgr = null; try { searcher = new IndexSearcher(indexDir); PerFieldAnalyzerWrapper a = LuceneManager.buildAnalyzer(); writer = IndexWriterManager.getWriter(nindexDir, a, true); final int numDocs = searcher.getIndexReader().numDocs(); HashSet deldocs = new HashSet(); HashMap infos = new HashMap(); for (int i = 0; i < numDocs; i++) { Document doc = searcher.doc(i); String delprop = doc.get(DeletedInfos.DELETED); String layerStr = doc.get(LuceneManager.LAYER_OF_SAVE); int layer = -1; try { layer = Integer.parseInt(layerStr); } catch (Exception ex) { layer = -1; } final String id = doc.get(LuceneManager.ID) + DeletedInfos.KEY_SEPERATOR + doc.get(LuceneManager.LAYER_OF_SAVE); if (delprop != null && "true".equals(delprop) /* && layer == DbKey.LIVE_LAYER*/) { deldocs.add(id); } else { Object v; if ((v = infos.get(id)) == null) { infos.put(id, new Integer(i)); } else { final String lmod = doc.get(LuceneManager.LASTMODIFIED); final String lmod_prev = searcher.doc(((Integer) v).intValue()).get("_lastmodified"); if (lmod_prev == null || (lmod != null && lmod.compareTo(lmod_prev) > 0)) { infos.put(id, new Integer(i)); } } } } ArrayList listOfMaps = new ArrayList(); for (int i = 0; i < numDocs; i++) { Document doc = searcher.doc(i); String delprop = doc.get(DeletedInfos.DELETED); String layerStr = doc.get(LuceneManager.LAYER_OF_SAVE); int layer = -1; try { layer = Integer.parseInt(layerStr); } catch (Exception ex) { layer = -1; } final String id = doc.get(LuceneManager.ID) + DeletedInfos.KEY_SEPERATOR + doc.get(LuceneManager.LAYER_OF_SAVE); if (delprop != null && "true".equals(delprop)) { continue; } else if (id != null && deldocs.contains(id) /* && layer == DbKey.LIVE_LAYER*/) { continue; } Integer idx = (Integer) infos.get(id); if (idx != null && i != idx.intValue()) { continue; } Document ndoc = convertDocument(doc); if (this.recordNodes) { listOfMaps.add(LuceneManager.luceneDocumentToMap(doc)); } if (ndoc != null) { writer.addDocument(ndoc); } } if (this.recordNodes) { lmgr = new LuceneManager(this.app, false, true); this.allNodes = new HashMap(); final int size = listOfMaps.size(); for (int i = 0; i < size; i++) { HashMap m = (HashMap) listOfMaps.get(i); INode n = lmgr.mapToNode(m); this.allNodes.put(n.getID(), getPath(n)); n = null; } } } catch (Exception ex) { ex.printStackTrace(); throw new RuntimeException(ex); } finally { if (searcher != null) { try { searcher.close(); } catch (Exception ex) { app.logError(ErrorReporter.errorMsg(this.getClass(), "convert"), ex); } } if (lmgr != null) { lmgr.shutdown(); lmgr = null; } indexDir.close(); SegmentInfos sinfos = IndexObjectsFactory.getFSSegmentInfos(indexDir); sinfos.clear(); IndexObjectsFactory.removeDeletedInfos(indexDir); } Connection conn = null; boolean exceptionOccured = false; try { if (writer != null) { TransSource ts = app.getTransSource(); conn = ts.getConnection(); DatabaseMetaData dmd = conn.getMetaData(); ResultSet rs = dmd.getColumns(null, null, "Lucene", "version"); if (!rs.next()) { final String alterTbl = "ALTER TABLE Lucene ADD version INT NOT NULL DEFAULT 1"; PreparedStatement pstmt = null; try { pstmt = conn.prepareStatement(alterTbl); pstmt.execute(); } catch (SQLException sqle) { app.logError(ErrorReporter.errorMsg(this.getClass(), "convert"), sqle); } finally { if (pstmt != null) { pstmt.close(); pstmt = null; } } } rs.close(); rs = null; writer.close(); writer.flushCache(); // TODO:writer.writeSegmentsFile(); LuceneManager.commitSegments(conn, app, writer.getDirectory()); writer.finalizeTrans(); this.updateSQL(conn); } } catch (Exception ex) { ex.printStackTrace(); exceptionOccured = true; throw new RuntimeException(ex); } finally { if (conn != null) { try { if (!conn.getAutoCommit()) { if (!exceptionOccured) { conn.commit(); } else { conn.rollback(); } } conn.close(); } catch (Exception ex) { app.logError(ErrorReporter.errorMsg(this.getClass(), "convert"), ex); } conn = null; } nindexDir.close(); SegmentInfos sinfos = IndexObjectsFactory.getFSSegmentInfos(nindexDir); sinfos.clear(); IndexObjectsFactory.removeDeletedInfos(nindexDir); } if (!dbhome.renameTo(olddbhome)) { throw new Exception("Could not move the old version of the db into " + olddbhome); } if (!ndbhome.renameTo(dbhome)) { throw new Exception("Could not move the newer version of the db into " + dbhome); } File oldBlobDir = new File(olddbhome, "blob"); File newBlobDir = new File(ndbhome, "blob"); oldBlobDir.renameTo(newBlobDir); if (!FileUtils.deleteDir(olddbhome)) { throw new Exception("Could not delete the old version of the db at " + olddbhome); } }