001: /*******************************************************************************
002: * JBoss, Home of Professional Open Source
003: * Copyright 2006, JBoss Inc., and individual contributors as indicated
004: * by the @authors tag. See the copyright.txt in the distribution for a
005: * full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: *******************************************************************************/package org.drools.resource.util;
022:
023: import java.io.ByteArrayOutputStream;
024: import java.io.File;
025: import java.io.FileInputStream;
026: import java.io.IOException;
027: import java.io.InputStream;
028: import java.util.ArrayList;
029: import java.util.Collection;
030: import java.util.Collections;
031: import java.util.HashMap;
032: import java.util.Iterator;
033: import java.util.List;
034: import java.util.Map;
035:
036: import org.apache.log4j.Logger;
037: import org.drools.resource.RepositoryBean;
038: import org.tmatesoft.svn.core.SVNDirEntry;
039: import org.tmatesoft.svn.core.SVNException;
040: import org.tmatesoft.svn.core.SVNNodeKind;
041: import org.tmatesoft.svn.core.SVNURL;
042: import org.tmatesoft.svn.core.auth.ISVNAuthenticationManager;
043: import org.tmatesoft.svn.core.internal.io.dav.DAVRepositoryFactory;
044: import org.tmatesoft.svn.core.internal.io.fs.FSRepositoryFactory;
045: import org.tmatesoft.svn.core.internal.io.svn.SVNRepositoryFactoryImpl;
046: import org.tmatesoft.svn.core.io.SVNRepository;
047: import org.tmatesoft.svn.core.io.SVNRepositoryFactory;
048: import org.tmatesoft.svn.core.wc.SVNWCUtil;
049:
050: /**
051: * Subversion utility class that uses JavaSVN to get rules and rule lists from
052: * Subversion via http. It is assumed that you have a working SVN Repository
053: * that is URL enabled. This class does support credentials, so the repository
054: * doesn't have to be setup with anonymous access, although that's the easiest
055: * to get working.
056: *
057: * @author James Williams
058: *
059: */
060: public class SvnUtil {
061: private static Logger logger = Logger.getLogger(SvnUtil.class);
062:
063: /**
064: * Get a DRL file's contents from Subversion.
065: *
066: * @param svnUrl -
067: * URL
068: * @param svnUsername -
069: * username
070: * @param svnPassword -
071: * password
072: * @param ruleVersion -
073: * tag or trunk SVN folder, which represents the subdirectory
074: * that contains the DRL.
075: * @param drlName -
076: * DRL file name
077: * @return
078: * @throws SVNException
079: */
080: public static ByteArrayOutputStream getFileContentsFromSvn(
081: String url, String svnUsername, String svnPassword,
082: long version) throws SVNException {
083: String username = svnUsername;
084: String password = svnPassword;
085: ByteArrayOutputStream baos = new ByteArrayOutputStream();
086:
087: setupLibrary();
088:
089: try {
090: SVNRepository repository = null;
091:
092: /*
093: * Creates an instance of SVNRepository to work with the repository.
094: * All user's requests to the repository are relative to the
095: * repository location used to create this SVNRepository. SVNURL is
096: * a wrapper for URL strings that refer to repository locations.
097: */
098: repository = SVNRepositoryFactoryImpl.create(SVNURL
099: .parseURIEncoded(url));
100:
101: /*
102: * User's authentication information is provided via an
103: * ISVNAuthenticationManager instance. SVNWCUtil creates a default
104: * usre's authentication manager given user's name and password.
105: */
106: ISVNAuthenticationManager authManager = SVNWCUtil
107: .createDefaultAuthenticationManager(username,
108: password);
109:
110: /*
111: * Sets the manager of the user's authentication information that
112: * will be used to authenticate the user to the server (if needed)
113: * during operations handled by the SVNRepository.
114: */
115: repository.setAuthenticationManager(authManager);
116:
117: /*
118: * This Map will be used to get the file properties. Each Map key is
119: * a property name and the value associated with the key is the
120: * property value.
121: */
122: Map fileProperties = new HashMap();
123:
124: /*
125: * Checks up if the specified path really corresponds to a file. If
126: * doesn't the program exits. SVNNodeKind is that one who says what
127: * is located at a path in a revision. -1 means the latest revision.
128: */
129: SVNNodeKind nodeKind = repository.checkPath("", -1);
130:
131: if (nodeKind == SVNNodeKind.NONE) {
132: logger.error("There is no entry at '" + url + "'.");
133:
134: } else if (nodeKind == SVNNodeKind.DIR) {
135: logger
136: .error("The entry at '"
137: + url
138: + "' is a directory while a file was expected.");
139:
140: }
141:
142: /*
143: * Gets the contents and properties of the file located at filePath
144: * in the repository at the latest revision (which is meant by a
145: * negative revision number).
146: */
147: repository.getFile("", version, fileProperties, baos);
148:
149: } catch (SVNException e) {
150: e.printStackTrace();
151: logger.error("The SVN file cannot be found. " + url);
152: throw e;
153: }
154:
155: return baos;
156: }
157:
158: /**
159: * Get a list of rule repository resources from subversion.
160: *
161: * @param criteriaBean
162: * @return
163: * @throws SVNException
164: */
165: public static List getRepositoryBeanList(
166: RepositoryBean criteriaBean, String username,
167: String password, String repositoryUrl)
168: throws RuntimeException {
169: String url = repositoryUrl;
170: List resourceList = new ArrayList();
171:
172: logger.debug("URL is: " + url);
173: /*
174: * initializes the library (it must be done before ever using the
175: * library itself)
176: */
177: setupLibrary();
178: try {
179: SVNRepository repository = SVNRepositoryFactory
180: .create(SVNURL.parseURIEncoded(url));
181: // we need to narrow down collection list to resources in a folder
182: // before
183: // this call
184: Collection entries = repository.getDir(url, -1, null,
185: (Collection) null);
186:
187: Collections.sort(resourceList);
188: Iterator i = entries.iterator();
189: while (i.hasNext()) {
190: String svnDetail = ((SVNDirEntry) i.next()).toString();
191: // here is where we need to insert criteria logic
192: }
193: } catch (SVNException e) {
194: logger.error("Repository does not exist or is unavailable");
195: throw new RuntimeException(e);
196: }
197:
198: return resourceList;
199: }
200:
201: /**
202: * Write the DRL file out to a String.
203: *
204: * @param baos
205: * @return
206: */
207: public static String writeFile(ByteArrayOutputStream baos) {
208: String fileText = null;
209: try {
210: fileText = baos.toString("text/html");
211: } catch (IOException ioe) {
212: logger.error(ioe);
213: }
214:
215: return fileText;
216: }
217:
218: public static ByteArrayOutputStream getByteArrayOutputFromFile(
219: File file) throws IOException {
220: InputStream is = new FileInputStream(file);
221:
222: // Get the size of the file
223: long length = file.length();
224:
225: // You cannot create an array using a long type.
226: // It needs to be an int type.
227: // Before converting to an int type, check
228: // to ensure that file is not larger than Integer.MAX_VALUE.
229: if (length > Integer.MAX_VALUE) {
230: // File is too large
231: }
232:
233: // Create the byte array to hold the data
234: byte[] bytes = new byte[(int) length];
235:
236: // Read in the bytes
237: int offset = 0;
238: int numRead = 0;
239: while (offset < bytes.length
240: && (numRead = is.read(bytes, offset, bytes.length
241: - offset)) >= 0) {
242: offset += numRead;
243: }
244:
245: // Ensure all the bytes have been read in
246: if (offset < bytes.length) {
247: throw new IOException("Could not completely read file "
248: + file.getName());
249: }
250:
251: // Close the input stream and return bytes
252: is.close();
253: ByteArrayOutputStream baos = new ByteArrayOutputStream();
254: baos.write(bytes);
255: return baos;
256: }
257:
258: /*
259: * Initializes the library to work with a repository either via svn:// (and
260: * svn+ssh://) or via http:// (and https://)
261: */
262: public static void setupLibrary() {
263: // for DAV (over http and https)
264: DAVRepositoryFactory.setup();
265:
266: // for SVN (over svn and svn+ssh)
267: SVNRepositoryFactoryImpl.setup();
268:
269: // For File
270: FSRepositoryFactory.setup();
271: }
272:
273: /**
274: * authenticate a subversion user for access to a particular URL.
275: *
276: * @param criteriaBean
277: * @return
278: * @throws SVNException
279: */
280: public static boolean authenticate(String username,
281: String password, String repositoryUrl)
282: throws RuntimeException {
283:
284: try {
285: logger.debug("Subversion authentication for : username="
286: + username + ", password=" + password
287: + ", repositoryURL=" + repositoryUrl);
288:
289: setupLibrary();
290: SVNRepository repository = null;
291:
292: /*
293: * Creates an instance of SVNRepository to work with the repository.
294: * All user's requests to the repository are relative to the
295: * repository location used to create this SVNRepository. SVNURL is
296: * a wrapper for URL strings that refer to repository locations.
297: */
298: repository = SVNRepositoryFactory.create(SVNURL
299: .parseURIEncoded(repositoryUrl));
300:
301: /*
302: * User's authentication information is provided via an
303: * ISVNAuthenticationManager instance. SVNWCUtil creates a default
304: * usre's authentication manager given user's name and password.
305: */
306: ISVNAuthenticationManager authManager = SVNWCUtil
307: .createDefaultAuthenticationManager(username,
308: password);
309:
310: /*
311: * Sets the manager of the user's authentication information that
312: * will be used to authenticate the user to the server (if needed)
313: * during operations handled by the SVNRepository.
314: */
315: repository.setAuthenticationManager(authManager);
316: repository.testConnection();
317: } catch (SVNException e) {
318: logger.error(e.getErrorMessage());
319: return false;
320: }
321:
322: return true;
323: }
324:
325: }
|