Created
March 4, 2012 14:50
-
-
Save lindenb/1973359 to your computer and use it in GitHub Desktop.
Java Remote Method Invocation (RMI) for Bioinformatics
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
grant { | |
permission java.security.AllPermission; | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
./sandbox/client/FirstBases.java | |
./sandbox/client/GCPercent.java | |
./sandbox/client/SequenceAnalyzerClient.java | |
./sandbox/server/SequenceAnalyzerServiceImpl.java | |
./sandbox/shared/SequenceAnalyzerService.java | |
./sandbox/shared/SequenceAnalyzer.java | |
./client.policy | |
./server.policy |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package sandbox.client; | |
import java.io.Serializable; | |
import sandbox.shared.SequenceAnalyzer; | |
public class FirstBases implements SequenceAnalyzer | |
{ | |
private int count=0; | |
private transient StringBuilder sequence=null; | |
public FirstBases() | |
{ | |
} | |
public int getCount() | |
{ | |
return count; | |
} | |
public void setCount(int count) | |
{ | |
this.count=count; | |
} | |
@Override | |
public void characters(char content[],int pos,int length) | |
{ | |
if(sequence==null) sequence=new StringBuilder(getCount()); | |
for(int i=0;i< length && sequence.length() < getCount();++i) | |
{ | |
sequence.append(content[i+pos]); | |
} | |
} | |
@Override | |
public Serializable getResult() | |
{ | |
if(sequence==null) return null; | |
return sequence.toString(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package sandbox.client; | |
import java.io.Serializable; | |
import sandbox.shared.SequenceAnalyzer; | |
public class GCPercent implements SequenceAnalyzer | |
{ | |
private transient double total=0; | |
private transient double gc=0; | |
public GCPercent() | |
{ | |
} | |
@Override | |
public void characters(char content[],int pos,int length) | |
{ | |
total+=length; | |
for(int i=0;i< length;++i) | |
{ | |
switch(content[pos+i]) | |
{ | |
case 'G':case 'C': | |
case 'g':case 'c': | |
case 's': gc++; | |
default:break; | |
} | |
} | |
} | |
@Override | |
public Serializable getResult() | |
{ | |
if(total==0) return null; | |
return total/gc; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
server: | |
javac -cp . sandbox/server/SequenceAnalyzerServiceImpl.java | |
client: | |
javac -cp . sandbox/client/SequenceAnalyzerClient.java |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package sandbox.shared; | |
import java.io.Serializable; | |
public interface SequenceAnalyzer extends Serializable | |
{ | |
public void characters(char content[],int pos,int length); | |
public Serializable getResult(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package sandbox.client; | |
import java.rmi.registry.LocateRegistry; | |
import java.rmi.registry.Registry; | |
import sandbox.shared.*; | |
public class SequenceAnalyzerClient | |
{ | |
public static void main(String args[]) throws Exception | |
{ | |
/* the process of receiving the server remote object's stub | |
could require downloading class definitions from the server. */ | |
if (System.getSecurityManager() == null) | |
{ | |
System.setSecurityManager(new SecurityManager()); | |
} | |
Registry registry = LocateRegistry.getRegistry(args[0]); | |
SequenceAnalyzerService comp = (SequenceAnalyzerService) registry.lookup(SequenceAnalyzerService.SERVICE_NAME); | |
for(int gi=25;gi<30;++gi) | |
{ | |
GCPercent analyzer1=new GCPercent(); | |
FirstBases analyzer2=new FirstBases(); | |
analyzer2.setCount(5+gi%7); | |
System.err.println("gi="+gi+" gc%="+comp.analyse(gi,analyzer1)); | |
System.err.println("gi="+gi+" start="+comp.analyse(gi,analyzer2)); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package sandbox.shared; | |
import java.rmi.Remote; | |
import java.rmi.RemoteException; | |
import java.io.IOException; | |
import org.xml.sax.SAXException; | |
public interface SequenceAnalyzerService extends Remote | |
{ | |
public static final String SERVICE_NAME="efetch"; | |
public java.io.Serializable analyse(int gi,SequenceAnalyzer analyzer) throws IOException,SAXException; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package sandbox.server; | |
import java.rmi.RemoteException; | |
import java.rmi.registry.LocateRegistry; | |
import java.rmi.registry.Registry; | |
import java.rmi.server.UnicastRemoteObject; | |
import java.io.Serializable; | |
import java.io.IOException; | |
import javax.xml.parsers.SAXParserFactory; | |
import javax.xml.parsers.SAXParser; | |
import org.xml.sax.*; | |
import org.xml.sax.helpers.DefaultHandler; | |
import org.xml.sax.SAXException; | |
import sandbox.shared.*; | |
public class SequenceAnalyzerServiceImpl implements SequenceAnalyzerService | |
{ | |
static private class Handler extends DefaultHandler | |
{ | |
boolean inSeq=false; | |
SequenceAnalyzer analyzer; | |
public void startElement(String uri, | |
String localName, | |
String qName, | |
Attributes attributes) throws SAXException | |
{ | |
if(qName.equals("TSeq_sequence")) inSeq=true; | |
} | |
public void characters(char[] ch, | |
int start, | |
int length) throws SAXException | |
{ | |
if(!inSeq) return; | |
analyzer.characters(ch,start,length); | |
} | |
public void endElement(String uri, | |
String localName, | |
String qName) throws SAXException | |
{ | |
inSeq=false; | |
} | |
} | |
public SequenceAnalyzerServiceImpl() | |
{ | |
} | |
@Override | |
public Serializable analyse(int gi,SequenceAnalyzer analyzer) throws IOException,SAXException | |
{ | |
try | |
{ | |
SAXParserFactory factory = SAXParserFactory.newInstance(); | |
SAXParser saxParser = factory.newSAXParser(); | |
Handler handler=new Handler(); | |
handler.analyzer=analyzer; | |
saxParser.parse( | |
"http://eutils.ncbi.nlm.nih.gov/entrez/eutils/efetch.fcgi?"+ | |
"db=nucleotide&rettype=fasta&retmode=xml&id="+gi, handler); | |
return analyzer.getResult(); | |
} | |
catch(javax.xml.parsers.ParserConfigurationException err) | |
{ | |
throw new RuntimeException(err); | |
} | |
} | |
public static void main(String[] args) | |
{ | |
/* protects access to system resources from untrusted downloaded | |
code running within the Java virtual machine. */ | |
if (System.getSecurityManager() == null) | |
{ | |
System.setSecurityManager(new SecurityManager()); | |
} | |
try | |
{ | |
SequenceAnalyzerService engine = new SequenceAnalyzerServiceImpl(); | |
/* exports the supplied remote object so that it can | |
receive invocations of its remote methods from remote clients. */ | |
SequenceAnalyzerService stub = (SequenceAnalyzerService) UnicastRemoteObject.exportObject( | |
engine, | |
0 //TCP port | |
); | |
/* adds the name to the RMI registry running on the server */ | |
Registry registry = LocateRegistry.getRegistry(); | |
registry.rebind(SequenceAnalyzerService.SERVICE_NAME, stub); | |
System.out.println("SequenceAnalyzerService bound."); | |
} | |
catch (Exception e) | |
{ | |
e.printStackTrace(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
grant { | |
permission java.security.AllPermission; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment