001: /*
002: * Created on 04/05/2004
003: *
004: * Swing Components - visit http://sf.net/projects/gfd
005: *
006: * Copyright (C) 2004 Igor Regis da Silva Simões
007: *
008: * This program is free software; you can redistribute it and/or
009: * modify it under the terms of the GNU General Public License
010: * as published by the Free Software Foundation; either version 2
011: * of the License, or (at your option) any later version.
012: *
013: * This program is distributed in the hope that it will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
016: * GNU General Public License for more details.
017: *
018: * You should have received a copy of the GNU General Public License
019: * along with this program; if not, write to the Free Software
020: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
021: */
022: package br.com.gfp.actions;
023:
024: import java.awt.Component;
025: import java.awt.event.ActionEvent;
026: import java.awt.event.ActionListener;
027: import java.io.DataInputStream;
028: import java.io.File;
029: import java.io.FileInputStream;
030: import java.io.FileNotFoundException;
031: import java.io.FileOutputStream;
032: import java.io.IOException;
033: import java.sql.SQLException;
034: import java.util.Enumeration;
035: import java.util.Properties;
036: import java.util.jar.JarOutputStream;
037: import java.util.zip.ZipEntry;
038: import java.util.zip.ZipFile;
039:
040: import javax.swing.JFileChooser;
041: import javax.swing.JFrame;
042: import javax.swing.JOptionPane;
043: import javax.swing.SwingUtilities;
044:
045: import SevenZip.LzmaAlone;
046: import br.com.gfp.dao.GFPController;
047: import br.com.gfp.dao.PluginDAO;
048: import br.com.gfp.data.Plugin;
049: import br.com.gfp.db.MigradorDB;
050: import br.com.gfp.internationalization.ActionsMessages;
051: import br.com.gfp.util.SimpleLog;
052: import br.com.gfpshare.util.FileManager;
053: import br.com.gfpshare.util.MyFileFilter;
054:
055: import com.sun.java.util.jar.pack.UnpackerImpl;
056:
057: /**
058: * @author Igor Regis da Silva Simoes
059: */
060: public class InstalarPluginsAction implements ActionListener {
061: private static InstalarPluginsAction action = null;
062:
063: private JFileChooser chooser = null;
064:
065: /**
066: *
067: */
068: private InstalarPluginsAction() {
069: super ();
070: chooser = new JFileChooser();
071: chooser.setDialogTitle(ActionsMessages.getMessages().getString(
072: "tituloInstallPlugin"));
073: chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
074: chooser.setFileFilter(new MyFileFilter("gfz", "GFP Plugin"));
075: }
076:
077: /**
078: * @return Retorna uma instancia desta action
079: */
080: public static InstalarPluginsAction getAction() {
081: return action;
082: }
083:
084: /**
085: *
086: */
087: public static void initializeAction() {
088: if (action == null)
089: action = new InstalarPluginsAction();
090: }
091:
092: /**
093: * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
094: */
095: public void actionPerformed(ActionEvent e) {
096: chooser.updateUI();
097: if (chooser.showOpenDialog((JFrame) GFPController
098: .getGFPController().getContexto().get(
099: GFPController.FRAME_PRINCIPAL)) == JFileChooser.APPROVE_OPTION) {
100: install(chooser.getSelectedFile(), e);
101: }
102: }
103:
104: private void install(final File file, final ActionEvent e) {
105: (new Thread() {
106: @Override
107: public void run() {
108: try {
109: instala(desempacota(file));
110:
111: SwingUtilities.invokeLater(new Runnable() {
112: public void run() {
113: int ret = JOptionPane
114: .showOptionDialog(
115: (Component) GFPController
116: .getGFPController()
117: .getContexto()
118: .get(
119: GFPController.FRAME_PRINCIPAL),
120: ActionsMessages
121: .getMessages()
122: .getString(
123: "PrecisaReiniciar")
124: + "\n"
125: + ActionsMessages
126: .getMessages()
127: .getString(
128: "querMesmoSair"),
129: ActionsMessages
130: .getMessages()
131: .getString(
132: "atencao"),
133: JOptionPane.YES_NO_OPTION,
134: JOptionPane.INFORMATION_MESSAGE,
135: null,
136: new Object[] {
137: ActionsMessages
138: .getMessages()
139: .getString(
140: "sim"),
141: ActionsMessages
142: .getMessages()
143: .getString(
144: "nao") },
145: ActionsMessages
146: .getMessages()
147: .getString("nao"));
148: if (ret == 0) {
149: SairAction.getAction().actionPerformed(
150: null);
151: }
152: }
153: });
154: } catch (Exception e) {
155: ((SimpleLog) GFPController.getGFPController()
156: .getContexto().get(GFPController.LOG))
157: .log("Erro ao instalar plugins: " + file);
158: ((SimpleLog) GFPController.getGFPController()
159: .getContexto().get(GFPController.LOG))
160: .log(e.getLocalizedMessage());
161: ((SimpleLog) GFPController.getGFPController()
162: .getContexto().get(GFPController.LOG))
163: .log(e);
164: }
165: }
166: }).start();
167: }
168:
169: private String desempacota(File file) throws FileNotFoundException,
170: IOException {
171: ZipEntry ze = null;
172: ZipFile zf = new ZipFile(file);
173: Enumeration entries = zf.entries();
174: String pluginDir = null;
175: while (entries.hasMoreElements()) {
176: ze = (ZipEntry) entries.nextElement();
177: DataInputStream fonte = new DataInputStream(zf
178: .getInputStream(ze));
179: byte[] data = new byte[fonte.available()];
180:
181: fonte.readFully(data);
182: fonte.close();
183: String path = System.getProperty("user.dir")
184: + File.separator + "plugins" + File.separator
185: + ze.getName().replace('/', File.separatorChar);
186: if (path.endsWith(File.separator)) {
187: pluginDir = path;
188: File dir = new File(path);
189: if (dir.exists())
190: System.out.println(dir.renameTo(new File(path
191: + "Old")));
192: new File(path).mkdirs();
193: } else {
194: FileOutputStream fos = new FileOutputStream(path);
195: fos.write(data);
196: fos.flush();
197: fos.close();
198: if (path.indexOf("__") != -1)//Caso esteja packeado (pack200)
199: {
200: try {
201: LzmaAlone.main(new String[] { "d", path,
202: path + "_" });
203: } catch (Exception e) {
204: // TODO Auto-generated catch block
205: e.printStackTrace();
206: }
207: new File(path).delete();
208:
209: UnpackerImpl unpacker = new UnpackerImpl();
210: File pack = new File(path + "_");
211:
212: JarOutputStream jarOutputStream = new JarOutputStream(
213: new FileOutputStream(new File(path
214: .replaceAll("__", ""))));
215: unpacker.unpack(pack, jarOutputStream);
216: jarOutputStream.close();
217: pack.delete();
218: }
219: }
220: }
221: zf.close();
222: return pluginDir;
223: }
224:
225: private void instala(String pluginDir) throws IOException,
226: SQLException, VersaoMaisAtualException {
227: FileInputStream fis = new FileInputStream(pluginDir
228: + File.separator + "plugin.conf");
229: Properties conf = new Properties();
230: conf.load(fis);
231: fis.close();
232:
233: PluginDAO controller = new PluginDAO();
234: Plugin plugin = new Plugin(), pluginInstalado = new Plugin();
235:
236: plugin.setNome(conf.getProperty("name"));
237: pluginInstalado.setNome(conf.getProperty("name"));
238:
239: try {
240: pluginInstalado = controller.getBy(plugin);
241: } catch (SQLException e) {
242: plugin.setNome(conf.getProperty("name"));
243: plugin.setVersao(Double
244: .valueOf(conf.getProperty("release")));
245: plugin.setVersaoDB(Integer.valueOf(conf
246: .getProperty("db.release")));
247: plugin.setDescricao(conf.getProperty("description"));
248:
249: String temp = pluginDir.substring(pluginDir
250: .lastIndexOf("plugins")
251: + "pluginss".length());
252: temp = temp.substring(0, temp.length() - 1);
253:
254: plugin.setInstallDir(temp);
255: controller.adicionarNovo(plugin);
256: pluginInstalado.setDados(plugin.getAsMap());
257: }
258:
259: plugin.setVersao(Double.valueOf(conf.getProperty("release")));
260: plugin.setVersaoDB(Integer.valueOf(conf
261: .getProperty("db.release")));
262: plugin.setDescricao(conf.getProperty("description"));
263:
264: if (pluginInstalado.getVersao().doubleValue() > plugin
265: .getVersao().doubleValue()) {
266: FileManager.deleteTree(pluginDir);
267: new File(pluginDir + "Old").renameTo(new File(pluginDir));
268: throw new VersaoMaisAtualException("");
269: }
270:
271: if (pluginInstalado.getVersaoDB().intValue() < plugin
272: .getVersaoDB().intValue()) {
273: migraDBPlugin(conf.getProperty("namePackage"),
274: pluginInstalado.getVersaoDB().intValue());
275: }
276:
277: pluginInstalado.setDados(plugin.getAsMap());
278: controller.atualizar(pluginInstalado);
279: }
280:
281: private void migraDBPlugin(String pacote, int versaoAtual) {
282: new MigradorDB().migrarDB("br.com." + pacote
283: + ".db.migracao.MigrarDB", versaoAtual);
284: }
285:
286: private class VersaoMaisAtualException extends Exception {
287: /**
288: * Cria uma sceção para interronper a instalação do plugins atual
289: * @param message
290: */
291: public VersaoMaisAtualException(String message) {
292: super(message);
293: }
294: }
295: }
|