PageRenderTime 40ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 1ms

/projects/compiere-330/base/src/org/compiere/model/MMediaServer.java

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Java | 310 lines | 213 code | 23 blank | 74 comment | 18 complexity | 25db7e77cd36e1f8b49cf17d585b666d MD5 | raw file
  1. /******************************************************************************
  2. * Product: Compiere ERP & CRM Smart Business Solution *
  3. * Copyright (C) 1999-2007 ComPiere, Inc. All Rights Reserved. *
  4. * This program is free software, you can redistribute it and/or modify it *
  5. * under the terms version 2 of the GNU General Public License as published *
  6. * by the Free Software Foundation. This program is distributed in the hope *
  7. * that it will be useful, but WITHOUT ANY WARRANTY, without even the implied *
  8. * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. *
  9. * See the GNU General Public License for more details. *
  10. * You should have received a copy of the GNU General Public License along *
  11. * with this program, if not, write to the Free Software Foundation, Inc., *
  12. * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. *
  13. * For the text or an alternative of this public license, you may reach us *
  14. * ComPiere, Inc., 3600 Bridge Parkway #102, Redwood City, CA 94065, USA *
  15. * or via info@compiere.org or http://www.compiere.org/license.html *
  16. *****************************************************************************/
  17. package org.compiere.model;
  18. import java.io.*;
  19. import java.sql.*;
  20. import java.util.*;
  21. import java.util.logging.*;
  22. import org.apache.commons.net.ftp.*;
  23. import org.compiere.util.*;
  24. /**
  25. * Media Server Model
  26. *
  27. * @author Yves Sandfort
  28. */
  29. public class MMediaServer extends X_CM_Media_Server
  30. {
  31. /**
  32. *
  33. */
  34. private static final long serialVersionUID = 1L;
  35. /**
  36. * Get Media Server
  37. * @param project
  38. * @return server list
  39. */
  40. public static MMediaServer[] getMediaServer (MWebProject project)
  41. {
  42. ArrayList<MMediaServer> list = new ArrayList<MMediaServer>();
  43. PreparedStatement pstmt = null;
  44. String sql = "SELECT * FROM CM_Media_Server WHERE CM_WebProject_ID=? ORDER BY CM_Media_Server_ID";
  45. try
  46. {
  47. pstmt = DB.prepareStatement (sql, project.get_Trx());
  48. pstmt.setInt (1, project.getCM_WebProject_ID());
  49. ResultSet rs = pstmt.executeQuery ();
  50. while (rs.next ())
  51. {
  52. list.add (new MMediaServer (project.getCtx(), rs, project.get_Trx()));
  53. }
  54. rs.close ();
  55. pstmt.close ();
  56. pstmt = null;
  57. }
  58. catch (Exception e)
  59. {
  60. s_log.log (Level.SEVERE, sql, e);
  61. }
  62. try
  63. {
  64. if (pstmt != null)
  65. pstmt.close ();
  66. pstmt = null;
  67. }
  68. catch (Exception e)
  69. {
  70. pstmt = null;
  71. }
  72. MMediaServer[] retValue = new MMediaServer[list.size ()];
  73. list.toArray (retValue);
  74. return retValue;
  75. } // getMediaServer
  76. /** Logger */
  77. private static CLogger s_log = CLogger.getCLogger (MMediaServer.class);
  78. /**************************************************************************
  79. * Standard Constructor
  80. * @param ctx context
  81. * @param CM_Media_Server_ID id
  82. * @param trx transaction
  83. */
  84. public MMediaServer (Ctx ctx, int CM_Media_Server_ID, Trx trx)
  85. {
  86. super (ctx, CM_Media_Server_ID, trx);
  87. } // MMediaServer
  88. /**
  89. * Load Constructor
  90. * @param ctx context
  91. * @param rs request
  92. * @param trx transaction
  93. */
  94. public MMediaServer (Ctx ctx, ResultSet rs, Trx trx)
  95. {
  96. super (ctx, rs, trx);
  97. } // MMediaServer
  98. /** MWebProject WebProject (parent) */
  99. public MWebProject m_project = null;
  100. /**
  101. * getWebProject
  102. * @return Webproject (Parent)
  103. */
  104. public MWebProject getWebProject()
  105. {
  106. if (m_project==null)
  107. m_project = new MWebProject(getCtx(), getCM_WebProject_ID (), get_Trx ());
  108. return m_project;
  109. }
  110. /**
  111. * reDeployAll set all media items to redeploy
  112. */
  113. public void reDeployAll ()
  114. {
  115. MMedia[] media = MMedia.getMedia (getWebProject());
  116. if (media!=null && media.length>0)
  117. {
  118. for (MMedia element : media) {
  119. MMediaDeploy thisDeploy = MMediaDeploy.getByMedia (getCtx(), element.get_ID (), get_ID(), true, null);
  120. if (thisDeploy.isDeployed ())
  121. {
  122. log.log (Level.FINE, "Reset Deployed Flag on MediaItem" + element.get_ID());
  123. thisDeploy.setIsDeployed (false);
  124. thisDeploy.save ();
  125. }
  126. }
  127. }
  128. }
  129. /**
  130. * (Re-)Deploy all media
  131. * @return true if deployed
  132. */
  133. public boolean deploy ()
  134. {
  135. MMedia[] media = MMedia.getMediaToDeploy(getCtx(), this.get_ID (), get_Trx());
  136. // Check whether the host is our example localhost, we will not deploy locally, but this is no error
  137. if (this.getIP_Address().equals("127.0.0.1") || this.getName().equals("localhost")) {
  138. log.warning("You have not defined your own server, we will not really deploy to localhost!");
  139. return true;
  140. }
  141. FTPClient ftp = new FTPClient();
  142. try
  143. {
  144. ftp.connect (getIP_Address());
  145. if (ftp.login (getUserName(), getPassword()))
  146. log.info("Connected to " + getIP_Address() + " as " + getUserName());
  147. else
  148. {
  149. log.warning("Could NOT connect to " + getIP_Address() + " as " + getUserName());
  150. return false;
  151. }
  152. }
  153. catch (Exception e)
  154. {
  155. log.log(Level.WARNING, "Could NOT connect to " + getIP_Address()
  156. + " as " + getUserName(), e);
  157. return false;
  158. }
  159. boolean success = true;
  160. String cmd = null;
  161. // List the files in the directory
  162. try
  163. {
  164. cmd = "cwd";
  165. ftp.changeWorkingDirectory (getFolder());
  166. //
  167. cmd = "list";
  168. String[] fileNames = ftp.listNames();
  169. log.log(Level.FINE, "Number of files in " + getFolder() + ": " + fileNames.length);
  170. /*
  171. FTPFile[] files = ftp.listFiles();
  172. log.config("Number of files in " + getFolder() + ": " + files.length);
  173. for (int i = 0; i < files.length; i++)
  174. log.fine(files[i].getTimestamp() + " \t" + files[i].getName());*/
  175. //
  176. cmd = "bin";
  177. ftp.setFileType(FTP.BINARY_FILE_TYPE);
  178. //
  179. for (int i = 0; i < media.length; i++)
  180. {
  181. MMediaDeploy thisDeployment = MMediaDeploy.getByMedia (getCtx(), media[i].get_ID (), this.get_ID (), false, get_Trx());
  182. if (!media[i].isSummary() && media[i].getMediaType ()!=null) {
  183. log.log(Level.INFO, " Deploying Media Item: " + media[i].toString());
  184. MImage thisImage = media[i].getImage();
  185. // Open the file and output streams
  186. byte[] buffer = thisImage.getData();
  187. ByteArrayInputStream is = new ByteArrayInputStream(buffer);
  188. String fileName = media[i].get_ID() + media[i].getExtension();
  189. cmd = "put " + fileName;
  190. ftp.storeFile(fileName, is);
  191. is.close();
  192. thisDeployment.setIsDeployed (true);
  193. thisDeployment.save ();
  194. }
  195. }
  196. }
  197. catch (Exception e)
  198. {
  199. log.log(Level.WARNING, cmd, e);
  200. success = false;
  201. }
  202. // Logout from the FTP Server and disconnect
  203. try
  204. {
  205. cmd = "logout";
  206. ftp.logout();
  207. log.log(Level.FINE, " FTP logged out");
  208. cmd = "disconnect";
  209. ftp.disconnect();
  210. }
  211. catch (Exception e)
  212. {
  213. log.log(Level.WARNING, cmd, e);
  214. }
  215. ftp = null;
  216. return success;
  217. } // deploy
  218. /**
  219. * (Re-)Deploy all media
  220. * @return true if deployed
  221. */
  222. public boolean deleteMediaItem (MMedia t_media)
  223. {
  224. // Check whether the host is our example localhost, we will not deploy locally, but this is no error
  225. if (this.getIP_Address().equals("127.0.0.1") || this.getName().equals("localhost")) {
  226. log.warning("You have not defined your own server, we will not really deploy to localhost!");
  227. return true;
  228. }
  229. FTPClient ftp = new FTPClient();
  230. try
  231. {
  232. ftp.connect (getIP_Address());
  233. if (ftp.login (getUserName(), getPassword()))
  234. log.info("Connected to " + getIP_Address() + " as " + getUserName());
  235. else
  236. {
  237. log.warning("Could NOT connect to " + getIP_Address() + " as " + getUserName());
  238. return false;
  239. }
  240. }
  241. catch (Exception e)
  242. {
  243. log.log(Level.WARNING, "Could NOT connect to " + getIP_Address()
  244. + " as " + getUserName(), e);
  245. return false;
  246. }
  247. boolean success = true;
  248. String cmd = null;
  249. // List the files in the directory
  250. try
  251. {
  252. cmd = "cwd";
  253. ftp.changeWorkingDirectory (getFolder());
  254. //
  255. if (!t_media.isSummary()) {
  256. log.log(Level.INFO, " Deleting Media Item:" + t_media.get_ID() + t_media.getExtension());
  257. ftp.dele (t_media.get_ID() + t_media.getExtension());
  258. }
  259. }
  260. catch (Exception e)
  261. {
  262. log.log(Level.WARNING, cmd, e);
  263. success = false;
  264. }
  265. // Logout from the FTP Server and disconnect
  266. try
  267. {
  268. cmd = "logout";
  269. ftp.logout();
  270. cmd = "disconnect";
  271. ftp.disconnect();
  272. }
  273. catch (Exception e)
  274. {
  275. log.log(Level.WARNING, cmd, e);
  276. }
  277. ftp = null;
  278. return success;
  279. } // deploy
  280. @Override
  281. public String toString()
  282. {
  283. StringBuffer sb = new StringBuffer ("MMediaServer[")
  284. .append (get_ID()).append ("-").append (getName()).append ("]");
  285. return sb.toString ();
  286. }
  287. } // MMediaServer