@Override public byte[] getValue(Object instance) { if (data == null && file != null) { data = FileUtil.getFileBytes(file, null); } return data; }
/** * should be used, if the bean attribute stores the content of the attachment * @param id attribute id * @param source attachment content * @return attachment content */ public static byte[] getFileBytes(String id, Object source) { return FileUtil.getFileBytes(getValueFile(id, source).getPath(), null); }
/** * should be used, if the bean attribute stores only the file name of the attachment * @param instance bean values instance * @param attribute attribute name * @param file file name * @return attachment content */ public static byte[] getFileBytes(Object instance, String attribute, String file) { return FileUtil.getFileBytes(getFilename(instance, attribute, file), null); }
@Override public void run() { if (BeanContainer.isInitialized()) { EMessage.broadcast(this, "APPLICATION SHUTDOWN INITIALIZED...", "*"); LOG.info("preparing shutdown of local database " + persistence.getConnectionUrl()); try { BeanContainer.instance().executeStmt(ENV.get("app.shutdown.statement", "SHUTDOWN"), true, null); Thread.sleep(2000); } catch (Exception e) { LOG.error(e.toString()); } String hsqldbScript = isH2(persistence.getConnectionUrl()) ? persistence.getDefaultSchema() + ".mv.db" : persistence.getDatabase() + ".script"; String backupFile = ENV.getTempPath() + FileUtil.getUniqueFileName(ENV.get("app.database.backup.file", persistence.getDatabase()) + ".zip"); LOG.info("creating database backup to file " + backupFile); FileUtil.writeToZip(backupFile, hsqldbScript, FileUtil.getFileBytes(ENV.getConfigPath() + hsqldbScript, null)); } } }));
@Override public T getParsedValue(String source) { if (Attachment.isAttachment(this)) { //--> attachment, holding the data (not a file-name only) //first: load the transferred temporary file byte[] transferredBytes = FileUtil.getFileBytes(Attachment.getFilename(instance, getName(), source, true), null); //second: save that as new file - only for this instance! byte[] bytes = Attachment.getFileBytes(getId(), transferredBytes); //perhaps, convert the byte[] to an instanceof of Blob, InputStream,... return ByteUtil.toByteStream(bytes, getType()); } else { return super.getParsedValue(source); } }