Subversion Repositories XServices

Compare Revisions

No changes between revisions

Ignore whitespace Rev 93 → Rev 94

/xservices/trunk/src/java/net/brutex/xservices/util/CVSRoot.java
0,0 → 1,67
package net.brutex.xservices.util;
 
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
 
/**
* A struct containing the various bits of information in a CVS root string,
* allowing easy retrieval of individual items of information
*/
public class CVSRoot {
public String connectionType;
public String user;
public String host;
public String repository;
 
public CVSRoot(String root) throws IllegalArgumentException {
if (!root.startsWith(":"))
throw new IllegalArgumentException();
 
int oldColonPosition = 0;
int colonPosition = root.indexOf(':', 1);
if (colonPosition == -1)
throw new IllegalArgumentException();
connectionType = root.substring(oldColonPosition + 1, colonPosition);
oldColonPosition = colonPosition;
colonPosition = root.indexOf('@', colonPosition + 1);
if (colonPosition == -1)
throw new IllegalArgumentException();
user = root.substring(oldColonPosition + 1, colonPosition);
oldColonPosition = colonPosition;
colonPosition = root.indexOf(':', colonPosition + 1);
if (colonPosition == -1)
throw new IllegalArgumentException();
host = root.substring(oldColonPosition + 1, colonPosition);
repository = root.substring(colonPosition + 1);
if (connectionType == null || user == null || host == null
|| repository == null)
throw new IllegalArgumentException();
}
 
public String getCVSRoot(File directory) {
String root = null;
BufferedReader r = null;
try {
File rootFile = new File(directory, "CVS/Root");
if (rootFile.exists()) {
r = new BufferedReader(new FileReader(rootFile));
root = r.readLine();
}
} catch (IOException e) {
// ignore
} finally {
try {
if (r != null)
r.close();
} catch (IOException e) {
System.err.println("Warning: could not close CVS/Root file!");
}
}
if (root == null) {
root = System.getProperty("cvs.root");
}
return root;
}
}
Property changes:
Added: svn:mime-type
+text/plain
\ No newline at end of property
/xservices/trunk/src/java/net/brutex/xservices/util/cache/CacheExecutorService.java
0,0 → 1,56
/*
* Copyright 2012 Brian Rosenberger (Brutex Network)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
 
package net.brutex.xservices.util.cache;
 
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
 
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
 
/**
* @author Brian Rosenberger, bru(at)brutex.de
* @since 0.5.0-20120825
*/
public class CacheExecutorService implements ServletContextListener {
static final String EXECUTOR_NAME = "CACHE_EXECUTOR";
private ExecutorService executor;
 
public void contextInitialized(ServletContextEvent arg0) {
ServletContext context = arg0.getServletContext();
int nr_executors = 1;
ThreadFactory daemonFactory = new DaemonThreadFactory();
try {
nr_executors = Integer.parseInt(context.getInitParameter("cache:thread-count"));
} catch (NumberFormatException ignore ) {}
 
if(nr_executors <= 1) {
executor = Executors.newSingleThreadExecutor(daemonFactory);
} else {
executor = Executors.newFixedThreadPool(nr_executors,daemonFactory);
}
context.setAttribute(EXECUTOR_NAME, executor);
}
 
public void contextDestroyed(ServletContextEvent arg0) {
ServletContext context = arg0.getServletContext();
executor.shutdownNow(); // or process/wait until all pending jobs are done
}
 
}
Property changes:
Added: svn:mime-type
+text/plain
\ No newline at end of property
/xservices/trunk/src/java/net/brutex/xservices/util/cache/DaemonThreadFactory.java
0,0 → 1,52
/*
* Copyright 2012 Brian Rosenberger (Brutex Network)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
 
package net.brutex.xservices.util.cache;
 
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
 
public class DaemonThreadFactory implements ThreadFactory {
 
private final ThreadFactory factory;
 
/**
* Construct a ThreadFactory with setDeamon(true) using
* Executors.defaultThreadFactory()
*/
public DaemonThreadFactory() {
this(Executors.defaultThreadFactory());
}
 
/**
* Construct a ThreadFactory with setDeamon(true) wrapping the given factory
*
* @param thread
* factory to wrap
*/
public DaemonThreadFactory(ThreadFactory factory) {
if (factory == null)
throw new NullPointerException("factory cannot be null");
this.factory = factory;
}
 
public Thread newThread(Runnable r) {
final Thread t = factory.newThread(r);
t.setDaemon(true);
return t;
}
}
 
Property changes:
Added: svn:mime-type
+text/plain
\ No newline at end of property
/xservices/trunk/src/java/net/brutex/xservices/util/cache/CacheServlet.java
0,0 → 1,122
/*
* Copyright 2012 Brian Rosenberger (Brutex Network)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
 
package net.brutex.xservices.util.cache;
 
import java.io.File;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.concurrent.ExecutorService;
 
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.ws.rs.core.GenericEntity;
import javax.ws.rs.core.Response;
 
import org.apache.log4j.Logger;
 
import net.brutex.xservices.types.scm.ModuleType;
import net.brutex.xservices.ws.rs.CVSInfoImpl;
 
/**
* Perform Caching actions on the CVS Info actions
*
* @author Brian Rosenberger, bru(at)brutex.de
* @since 0.5.0-200120825
*
*/
public class CacheServlet extends HttpServlet {
 
private final Logger logger = Logger.getLogger(CacheServlet.class);
List<File> configfiles = new ArrayList<File>();;
int cacheinterval;
 
/*
* (non-Javadoc)
*
* @see javax.servlet.GenericServlet#init()
*/
@Override
public void init() throws ServletException {
super.init();
ExecutorService executor = (ExecutorService) getServletContext()
.getAttribute(CacheExecutorService.EXECUTOR_NAME);
 
Enumeration<String> attributes = getServletContext()
.getInitParameterNames();
while (attributes.hasMoreElements()) {
String name = attributes.nextElement();
if (name.startsWith("cvs-config-")) {
String configfile = (String) getServletContext()
.getInitParameter(name);
logger.info("CVS configuration file: " + configfile);
this.configfiles.add(new File(configfile));
}
}
cacheinterval = 15;
try {
cacheinterval = Integer.parseInt((String) getServletContext()
.getInitParameter("cvs-cache-interval"));
} catch (NumberFormatException e) {
logger.debug("Could not read parameter 'cvs-cache-interval' from web.xml. Using default value '"+cacheinterval+"' minutes");
}
logger.info("CacheServlet set to " + cacheinterval + " minutes interval.");
 
executor.submit(new Runnable() {
boolean isInterrupted = false;
 
@Override
public void run() {
while (!isInterrupted) {
for (File configfile : configfiles) {
CVSInfoImpl instance = new CVSInfoImpl();
logger.info("Caching modules from " + configfile.toURI().toString());
Response response = instance.getModules(null,
configfile, true);
List<ModuleType> list = (List<ModuleType>) ((GenericEntity) response
.getEntity()).getEntity();
if (list.size() == 0)
list.add(new ModuleType("", "", "", ""));
for (ModuleType t : list) {
try {
//Extra sleep
Thread.currentThread().sleep(5000);
} catch (InterruptedException e) {
isInterrupted = true;
break;
}
logger.info("Caching module '" + t.getName()+"'");
instance.getRepositoryFiles(null, configfile, t.getName(), true, true);
 
}
}
try {
logger.debug("Now sleeping for '"+cacheinterval+"' minutes");
Thread.currentThread().sleep(cacheinterval * 60000);
logger.debug("Waking up after '"+cacheinterval+"' minutes of sleep");
} catch (InterruptedException e) {
isInterrupted = true;
break;
}
}
 
}
});
 
}
 
}
Property changes:
Added: svn:mime-type
+text/plain
\ No newline at end of property
/xservices/trunk/src/java/net/brutex/xservices/util/BasicCVSListener.java
0,0 → 1,104
package net.brutex.xservices.util;
 
import java.io.PrintStream;
 
import org.netbeans.lib.cvsclient.command.FileInfoContainer;
import org.netbeans.lib.cvsclient.command.log.LogInformation;
import org.netbeans.lib.cvsclient.event.BinaryMessageEvent;
import org.netbeans.lib.cvsclient.event.CVSAdapter;
import org.netbeans.lib.cvsclient.event.CVSListener;
import org.netbeans.lib.cvsclient.event.FileAddedEvent;
import org.netbeans.lib.cvsclient.event.FileInfoEvent;
import org.netbeans.lib.cvsclient.event.FileRemovedEvent;
import org.netbeans.lib.cvsclient.event.FileToRemoveEvent;
import org.netbeans.lib.cvsclient.event.FileUpdatedEvent;
import org.netbeans.lib.cvsclient.event.MessageEvent;
import org.netbeans.lib.cvsclient.event.ModuleExpansionEvent;
import org.netbeans.lib.cvsclient.event.TerminationEvent;
 
public abstract class BasicCVSListener implements CVSListener
{
/**
* Stores a tagged line
*/
private final StringBuffer taggedLine = new StringBuffer();
 
/**
* Called when the server wants to send a message to be displayed to
* the user. The message is only for information purposes and clients
* can choose to ignore these messages if they wish.
* @param e the event
*/
public void messageSent(MessageEvent e)
{
String line = e.getMessage();
PrintStream stream = e.isError() ? System.err
: System.out;
 
if (e.isTagged())
{
String message = e.parseTaggedMessage(taggedLine, line);
// if we get back a non-null line, we have something
// to output. Otherwise, there is more to come and we
// should do nothing yet.
if (message != null)
{
//stream.println("Message: " + message);
}
}
else
{
//stream.println("Message: " + line);
}
}
 
@Override
public void commandTerminated(TerminationEvent arg0) {
}
 
@Override
public void fileAdded(FileAddedEvent arg0) {
}
 
@Override
public void fileInfoGenerated(FileInfoEvent arg0) {
FileInfoContainer info = arg0.getInfoContainer();
LogInformation info2 = (LogInformation) info;
System.out.println(info2.getRepositoryFilename());
System.out.println(info2.getDescription());
}
 
@Override
public void fileRemoved(FileRemovedEvent arg0) {
// TODO Auto-generated method stub
}
 
@Override
public void fileToRemove(FileToRemoveEvent arg0) {
// TODO Auto-generated method stub
}
 
@Override
public void fileUpdated(FileUpdatedEvent arg0) {
// TODO Auto-generated method stub
}
 
@Override
public void messageSent(BinaryMessageEvent arg0) {
// TODO Auto-generated method stub
}
 
@Override
public void moduleExpanded(ModuleExpansionEvent arg0) {
// TODO Auto-generated method stub
}
}
Property changes:
Added: svn:mime-type
+text/plain
\ No newline at end of property
/xservices/trunk/src/java/net/brutex/xservices/util/CVSClient.java
0,0 → 1,94
/*
* Copyright 2012 Brian Rosenberger (Brutex Network)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
 
package net.brutex.xservices.util;
 
import java.io.File;
 
 
import org.apache.commons.configuration.Configuration;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;
import org.netbeans.lib.cvsclient.Client;
import org.netbeans.lib.cvsclient.admin.StandardAdminHandler;
import org.netbeans.lib.cvsclient.command.CommandAbortedException;
import org.netbeans.lib.cvsclient.command.GlobalOptions;
import org.netbeans.lib.cvsclient.connection.AuthenticationException;
import org.netbeans.lib.cvsclient.connection.PServerConnection;
 
public class CVSClient {
private final File configfile;
private final PServerConnection connection;
private final CVSRoot root;
private final GlobalOptions globalOptions;
public final Client client;
public CVSClient(File config) throws CommandAbortedException, AuthenticationException, ConfigurationException {
 
if (config == null || !config.exists() || config.isDirectory())
throw new ConfigurationException("Config file not found");
this.configfile = config;
Configuration configuration = new PropertiesConfiguration(configfile);
 
String cvsroot = configuration.getString("CVSROOT");
String workdir = configuration.getString("WORKDIR");
String password = configuration.getString("PASSWORD");
this.root = new CVSRoot(cvsroot);
this.globalOptions = new GlobalOptions();
globalOptions.setCVSRoot(cvsroot);
this.connection = new PServerConnection();
connection.setUserName(root.user);
if (password != null) {
connection.setEncodedPassword(CvsPassword.encode(password));
} else {
connection.setEncodedPassword(password);
}
 
connection.setHostName(root.host);
connection.setRepository(root.repository);
connection.open();
 
this.client = new Client(connection, new StandardAdminHandler());
client.setLocalPath(workdir);
}
/**
* @return
*/
public File getConfigFile() {
return this.configfile;
}
public GlobalOptions getGlobalOptions() {
return this.globalOptions;
}
/**
* @return the connection
*/
public PServerConnection getConnection() {
return connection;
}
/**
* @return the root
*/
public CVSRoot getRoot() {
return root;
}
}
Property changes:
Added: svn:mime-type
+text/plain
\ No newline at end of property
/xservices/trunk/src/java/net/brutex/xservices/util/CvsPassword.java
0,0 → 1,125
/*
* Copyright 2012 Brian Rosenberger (Brutex Network)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
 
package net.brutex.xservices.util;
/*
* Copyright 2010 Andrew Kroh
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
 
 
 
/**
* A simple class for encoding and decoding passwords for CVS pserver protocol.
* Can be used to recover forgotten passwords.
*
* <p>
* Adapted from: http://blog.zmeeagain.com/2005/01/recover-cvs-pserver-passwords.html
*/
public class CvsPassword
{
private static final char[] LOOKUP_TABLE =
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 114, 120, 53,
79, 0, 109, 72, 108, 70, 64, 76, 67, 116, 74, 68, 87, 111, 52, 75,
119, 49, 34, 82, 81, 95, 65, 112, 86, 118, 110, 122, 105, 41, 57,
83, 43, 46, 102, 40, 89, 38, 103, 45, 50, 42, 123, 91, 35, 125, 55,
54, 66, 124, 126, 59, 47, 92, 71, 115, 78, 88, 107, 106, 56, 0,
121, 117, 104, 101, 100, 69, 73, 99, 63, 94, 93, 39, 37, 61, 48,
58, 113, 32, 90, 44, 98, 60, 51, 33, 97, 62, 77, 84, 80, 85};
 
/**
* Encodes a CVS password to be used in .cvspass file. Throws an exception
* if clearText is null, if a character is found outside the 0 - 126 range, or
* if within the range, one of the non-allowed characters.
*
* @param clearText
* the password in clear to be encoded
*
* @return the encoded cvs password
*/
public static String encode(String clearText)
{
// First character of encoded version is A:
char[] encoded = new char[clearText.length() + 1];
encoded[0] = 'A';
// Skip the first character:
int counter = 1;
for (char c : clearText.toCharArray())
{
if (c == '`' || c == '$' || c < 32)
{
throw new IllegalArgumentException(
"Illegal character was found in clear password.");
}
encoded[counter++] = LOOKUP_TABLE[c];
}
 
return String.valueOf(encoded);
}
 
/**
* Recovers an encoded via pserver protocol CVS password.
*
* @param encodedPassword
* the encoded password to be decoded
*
* @return the decoded password or null if the input was
* null or empty
*/
public static String decode(String encodedPassword)
{
String rtn = null;
if (encodedPassword != null && encodedPassword.length() > 0)
{
if (encodedPassword.startsWith("A"))
{
rtn = encode(encodedPassword.substring(1)).substring(1);
}
else
{
rtn = encode(encodedPassword).substring(1);
}
}
return rtn;
}
public static void main(String[] sArgs)
{
final String TEST_WORD = "i07w91";
String encoded = CvsPassword.encode(TEST_WORD);
System.out.println("Encoded: <" + encoded + ">");
String decoded = CvsPassword.decode(encoded);
System.out.println("Decoded: <" + decoded + ">");
System.out.println(decoded.equals(TEST_WORD) ? "Test Passed" : "Test Failed");
}
}
 
Property changes:
Added: svn:mime-type
+text/plain
\ No newline at end of property