KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > knowgate > scheduler > jobs > FTPPublisher


1 /*
2   Copyright (C) 2003 Know Gate S.L. All rights reserved.
3                       C/Oņa, 107 1š2 28050 Madrid (Spain)
4
5   Redistribution and use in source and binary forms, with or without
6   modification, are permitted provided that the following conditions
7   are met:
8
9   1. Redistributions of source code must retain the above copyright
10      notice, this list of conditions and the following disclaimer.
11
12   2. The end-user documentation included with the redistribution,
13      if any, must include the following acknowledgment:
14      "This product includes software parts from hipergate
15      (http://www.hipergate.org/)."
16      Alternately, this acknowledgment may appear in the software itself,
17      if and wherever such third-party acknowledgments normally appear.
18
19   3. The name hipergate must not be used to endorse or promote products
20      derived from this software without prior written permission.
21      Products derived from this software may not be called hipergate,
22      nor may hipergate appear in their name, without prior written
23      permission.
24
25   This library is distributed in the hope that it will be useful,
26   but WITHOUT ANY WARRANTY; without even the implied warranty of
27   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
28
29   You should have received a copy of hipergate License with this code;
30   if not, visit http://www.hipergate.org or mail to info@hipergate.org
31 */

32
33 package com.knowgate.scheduler.jobs;
34
35 import java.io.IOException JavaDoc;
36
37 import com.knowgate.debug.DebugFile;
38 import com.knowgate.dataobjs.DB;
39 import com.knowgate.scheduler.Atom;
40 import com.knowgate.scheduler.Job;
41 import com.knowgate.dataxslt.FastStreamReplacer;
42 import com.knowgate.dfs.FileSystem;
43
44 /**
45  * <p>Publishes Files via FTP</p>
46  * @author Sergio Montoro Ten
47  * @version 1.0
48  */

49
50 public class FTPPublisher extends Job {
51
52   // A reference to the replacer class witch maps tags of the form {#Section.Field}
53
// to their corresponding database fields.
54
private FastStreamReplacer oReplacer;
55
56   private FileSystem oFileSys;
57
58   public FTPPublisher() {
59     oReplacer = new FastStreamReplacer();
60     oFileSys = new FileSystem();
61   }
62
63   // ---------------------------------------------------------------------------
64

65   /**
66    * <p>Process PageSet pointed by Atom and sends transformed document throught FTP</p>
67    * <p>Base workareas path is taken from "workareasput" property of hipergate.cnf<p>
68    * @param oAtm Atom holding a reference to PageSet instance to be dumped<br>
69    * Atom must have the following parameters set:<br>
70    * <table border=1 cellpadding=4>
71    * <tr><td>gu_workarea</td><td>GUID of WorkArea owner of document to be sent</td></tr>
72    * <tr><td>gu_pageset</td><td>GUID of PageSet to be sent</td></tr>
73    * <tr><td>nm_pageset</td><td>Name of PageSet document instance to be sent</td></tr>
74    * <tr><td>tx_nickname</td><td>FTP User Nickname for login</td></tr>
75    * <tr><td>tx_pwd</td><td>FTP User Password for login</td></tr>
76    * <tr><td>nm_server</td><td>FTP server name or IP address</td></tr>
77    * <tr><td>path</td><td>FTP target path</td></tr>
78    * <tr><td>bo_mkpath</td><td>"1" if path must be created at target server</td></tr>
79    * <tr><td>nm_file</td><td>Target file name for nm_pageset</td></tr>
80    * </table>
81    * @return String containing the final pos-processed document
82    * @throws IOException
83    */

84
85   // ---------------------------------------------------------------------------
86

87   public void free() {}
88
89   public Object JavaDoc process(Atom oAtm) throws IOException JavaDoc {
90
91     String JavaDoc sPathHTML; // Full Path to Document Template File
92
char cBuffer[]; // Internal Buffer for Document Template File Data
93
Object JavaDoc oReplaced; // Document Template File Data after FastStreamReplacer processing
94

95     final String JavaDoc sSep = System.getProperty("file.separator"); // Alias for file.separator
96
final String JavaDoc Yes = "1";
97
98     if (DebugFile.trace) {
99       DebugFile.writeln("Begin FTPPublisher.process([Job:" + getStringNull(DB.gu_job,"") + ", Atom:" + String.valueOf(oAtm.getInt(DB.pg_atom)) + "])");
100       DebugFile.incIdent();
101     }
102
103
104     // *************************************************
105
// Compose the full path to document template file
106

107     // First get the storage base path from hipergate.cnf
108
sPathHTML = getProperty("workareasput");
109     if (!sPathHTML.endsWith(sSep)) sPathHTML += sSep;
110
111     // Concatenate PageSet workarea guid and subpath to Mailwire application directory
112
sPathHTML += getParameter("gu_workarea") + sSep + "apps" + sSep + "Mailwire" + sSep + "html" + sSep + getParameter("gu_pageset") + sSep;
113
114     // Concatenate PageSet Name
115
sPathHTML += getParameter("nm_pageset").replace(' ','_') + ".html";
116
117     if (DebugFile.trace) DebugFile.writeln("PathHTML = " + sPathHTML);
118
119     // *************************************************
120
// Call FastStreamReplacer for {#Section.Field} tags
121

122     oReplaced = oReplacer.replace(sPathHTML, oAtm.getItemMap());
123
124
125     // **************************************
126
// Send throught final replaced file data
127

128     oFileSys.user(getParameter("tx_nickname"));
129     oFileSys.password(getParameter("tx_pwd"));
130
131     try {
132
133       if (Yes.equals(getParameter("bo_path")))
134         oFileSys.mkdirs("ftp://" + getParameter("nm_server") + "/" + getParameter("path"));
135
136         oFileSys.copy( "file://" + sPathHTML,
137                        "ftp://" + getParameter("nm_server") + "/" + getParameter("path") + "/" + getParameter("nm_file"));
138
139     } catch (java.lang.Exception JavaDoc e) {
140       throw new IOException JavaDoc(e.getMessage());
141     }
142
143     // Decrement de count of atoms peding of processing at this job
144
iPendingAtoms--;
145
146     if (DebugFile.trace) {
147       DebugFile.writeln("End FTPPublisher.process([Job:" + getStringNull(DB.gu_job,"") + ", Atom:" + String.valueOf(oAtm.getInt(DB.pg_atom)) + "])");
148       DebugFile.decIdent();
149     }
150
151     return oReplaced;
152   } // process
153

154 } // FTPPublisher
155
Popular Tags