How to put in a proper file path in java Eclipse - java

I would like to know how to properly put in a text file and then convert it to an HTML file.
When I try putting a text file I get the errors
java.security.InvalidParameterException: Configuration file unreadable.
at com.outsideinsdk.ExportTest.parseConfig(ExportTest.java:51)
at com.outsideinsdk.ExportTest.<init>(ExportTest.java:35)
at com.outsideinsdk.ExportTest.main(ExportTest.java:197)
Sample code below
package com.outsideinsdk;
import com.outsideinsdk.*;
import java.io.*;
import java.security.*;
import java.util.*;
/**
* The <code>ExportTest</code> class tests the {#link Export Export} technology
* according to the properties provided in a given configuration file. The
* configuration file is assumed to be correctly formatted.
*
* #author Kevin Glannon
* #version 1.00
* #see Export Export
*/
public class ExportTest
{
private static final String INPUTPATHKEY = "inputpath";
private static final String OUTPUTPATHKEY = "outputpath";
private static final String OUTPUTIDKEY = "outputid";
Properties configProps = new Properties();
/**
* Since <code>ExportTest</code> objects are always associated with a
* configuration file, the constructor requires a configuration file path.
*
* #param cfp The configuration file path.
*/
public ExportTest(String cfp)
throws FileNotFoundException, IOException
{
parseConfig(cfp);
}
/**
* Parse the configuration file specified by the given path.
*
* #param cfp The configuration file path.
*/
public void parseConfig(String cfp)
throws FileNotFoundException, IOException
{
// Assure the configuration file exists and is readable.
File cff = new File(cfp);
if (!cff.exists() || !cff.isFile() || !cff.canRead())
{
throw(new InvalidParameterException("Configuration file unreadable."));
}
BufferedReader cfr = new BufferedReader(new FileReader(cff));
String line;
// Loop over all lines from the file.
while ((line = cfr.readLine()) != null)
{
processLine(line);
}
}
/**
* Support the parsing of the configuration file by processing a given
* line.
*
* #param l A line from a configuration file.
*/
private void processLine(String l)
{
// Look for comments.
int indPound = l.indexOf('#');
// Remove comments and whitespace.
String line = (indPound == -1) ? l.trim() :
l.substring(0, indPound).trim();
if (line.length() != 0)
{
StringTokenizer stl = new StringTokenizer(line);
String key = stl.nextToken();
String value = stl.nextToken();
while(stl.hasMoreTokens())
{
value +=" " + stl.nextToken();
}
// Fill in the appropriate property.
configProps.setProperty(key, value);
}
}
/**
* Run the conversion using the given input path, output path.
*
* #param ifp Input path.
* #param ofp Output path.
* #param timeout Export process timeout in milliseconds.
*/
public void convert(String ifp, String ofp, long timeout)
{
String oid = configProps.getProperty(OUTPUTIDKEY);
// Display the parameters.
System.out.println("Input Path: "+ifp+" Output Path: "+ofp+
" Output ID: "+oid);
// Remove extra control properties.
configProps.remove(INPUTPATHKEY);
configProps.remove(OUTPUTPATHKEY);
// Create list of input files.
File iff = new File(ifp);
File [] iffa;
if (iff.isDirectory())
iffa = iff.listFiles();
else
{
iffa = new File[1];
iffa[0] = iff;
}
// Create output directory if needed. Assuming that if the input path
// is a directory, the output path should also be a directory.
File off = new File(ofp);
if (iff.isDirectory() && !off.exists()) off.mkdir();
// Process the conversion.
Export e = new Export(configProps);
if (off.isDirectory())
{
// The outputid is in the form fi_XXX where XXX is a reasonable
// extension so we take the extension for the oid.
// oid.substring(3) means to get the string following the fi_
String ext = "." + oid.substring(3);
for (int i=0; i<iffa.length; i++)
{
String ifn = iffa[i].toString();
String ofn = ofp + File.separator + iffa[i].getName() + ext;
System.out.println("Converting "+ifn+" to "+ofn);
ExportStatusCode result = e.convert(ifn, ofn, oid, timeout);
if (result.getCode() == ExportStatusCode.SCCERR_OK.getCode())
{
System.out.println("Conversion Successful!");
}
else {
System.out.println("Conversion Error: " + result);
}
}
}
else
{
for (int i=0; i<iffa.length; i++)
{
ExportStatusCode result = e.convert(iffa[i].toString(), ofp, oid, timeout);
if (result.getCode() == ExportStatusCode.SCCERR_OK.getCode())
{
System.out.println("Conversion Successful!");
}
else {
System.out.println("Conversion Error: " + result);
}
}
}
}
/**
* Run the test according to the given arguments. These arguments must adhere to the following usage.<br><br>
* Usage:<br>
* ExportTest InputPath OutputPath ConfigurationFile [Timeout]<br><br>
*
* InputPath and OutputPath may be single files or directories. If InputPath is a directory, then all files in
* that directory will be converted, but without recursion. If OutputPath is a directory, then all converted
* files from InputPath are placed in the OutputPath directory by appending an extension which represents the
* output file type. Timeout is in milliseconds.
*
* #param args Command line arguments.
*/
public static void main(String[] args)
{
int count = args.length;
// Check for specification of configuration file.
if (count != 3 && count != 4)
{
System.out.println("Input path, output path and configuration file are required.");
System.out.println("Usage: ExportTest InputPath OutputPath "+
"ConfigurationFile [Timeout(in milliseconds)]");
System.out.println();
}
else
{
ExportTest ct = null;
try
{
ct = new ExportTest(args[2]);
}
catch (Exception ex)
{
ex.printStackTrace();
return;
}
long timeout = 0;
if( count == 4 )
{
timeout = Integer.decode( args[3] ).longValue();
}
ct.convert(args[0], args[1], timeout);
}
}
}
This is how my program arguments look like: here
This is where the yes.txt files are located in the project called explorer: here

Related

Listing files in ftp server without the use of recursion [duplicate]

I'm trying to produce file listing of a given directory and it's sub directories in a ftp server.
The server works fine, and I have been successfully able to produce the file listing of the current directory. When I try to list the subdirectories and their files is where it gets complicated.
I was asked not to use a recursion algorithm, so I did some research of my own. I have tried using threads (for every directory found, start a new thread), but I wasn't able to keep my connection stable and open. Any ideas on how to do so correctly with threads, or other alternatives?
EDIT: below is my code, when using the recursive statement (last line of code), it works
class TEST {
public static synchronized void main(String[] args) {
String server = args[0]; //server,path will be given as an arguments
String pass = "SOMEPASS";
String user = "SOMEUSER";
int port = 21;
FTPClient ftpClient = new FTPClient();
try {
ftpClient.connect(server, port);
showServerReply(ftpClient);
int replyCode = ftpClient.getReplyCode();
if (!FTPReply.isPositiveCompletion(replyCode)) {
System.out.println("Connect failed");
return;
}
boolean success = ftpClient.login(user, pass);
showServerReply(ftpClient);
if (!success) {
System.out.println("Could not login to the server");
return;
}
/*START THE FILE LISTING HERE*/
} catch (IOException ex) {
System.out.println("Oops! Something wrong happened");
ex.printStackTrace();
} finally {
// logs out and disconnects from server
try {
if (ftpClient.isConnected()) {
ftpClient.logout();
ftpClient.disconnect();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
private static void showServerReply(FTPClient ftpClient) {
String[] replies = ftpClient.getReplyStrings();
if (replies != null && replies.length > 0) {
for (String aReply : replies) {
System.out.println("SERVER: " + aReply);
}
}
}
private static void scanDir(FTPClient client, String path) throws IOException {
FTPFile[] files = client.listFiles(path); // Search all the files in the current directory
for (int j = 0; j < files.length; j++) {
System.out.println(files[j].getName()); // Print the name of each files
}
FTPFile[] directories = client.listDirectories(path); // Search all the directories in the current directory
for (int i = 0; i < directories.length; i++) {
String dirPath = directories[i].getName();
System.out.println(dirPath); // Print the path of a sub-directory
scanDir(client,dirPath); // Call recursively the method to display the files in the sub-directory DONT WANT TO DO THAT...
}
}
}
Okay, here is an example of how to handle it non-recursively, but with lists.
Mind, that this example is based on /accessing the local filesystem, but can easily be rewritten/extended for any kind of hierarchial/recursive structure.
package stackoverflow.nonrecursivefilesearch;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.stream.Stream;
public class NonRecursiveFileSearch {
public static void main(final String[] args) throws IOException {
final File searchDir = new File("D:\\test\\maven-test"); // set one
System.out.println("\nOld Java");
printDirs(listFiles_old(searchDir, true, true), "OLD: Depth first, include dirs");
printDirs(listFiles_old(searchDir, true, false), "OLD: Breadth first, include dirs");
printDirs(listFiles_old(searchDir, false, true), "OLD: Depth first, exclude dirs");
printDirs(listFiles_old(searchDir, false, false), "OLD: Breadth first, exclude dirs");
System.out.println("\nNew java.io with streams");
printDirs(listFiles_newIO(searchDir, true), "Java NIO, include dirs");
printDirs(listFiles_newIO(searchDir, false), "Java NIO, exclude dirs");
}
/**
* this is the way to 'manually' find files in hierarchial/recursive structures
*
* reminder: "Depth First" is not a real depth-first implementation
* real depth-first would iterate subdirs immediately.
* this implementation iterates breadth first, but descends into supdirs before it handles same-level directories
* advantage of this implementation is its speed, no need for additional lists etc.
*
* in case you want to exclude recursion traps made possible by symbolic or hard links, you could introduce a hashset/treeset with
* visited files (use filename strings retrieved with canonicalpath).
* in the loop, check if the current canonical filename string is contained in the hash/treeset
*/
static public ArrayList<File> listFiles_old(final File pDir, final boolean pIncludeDirectories, final boolean pDepthFirst) {
final ArrayList<File> found = new ArrayList<>();
final ArrayList<File> todo = new ArrayList<>();
todo.add(pDir);
while (todo.size() > 0) {
final int removeIndex = pDepthFirst ? todo.size() - 1 : 0;
final File currentDir = todo.remove(removeIndex);
if (currentDir == null || !currentDir.isDirectory()) continue;
final File[] files = currentDir.listFiles();
for (final File file : files) {
if (file.isDirectory()) {
if (pIncludeDirectories) found.add(file);
// additional directory filters go here
todo.add(file);
} else {
// additional file filters go here
found.add(file);
}
}
}
return found;
}
static private void printDirs(final ArrayList<File> pFiles, final String pTitle) {
System.out.println("====================== " + pTitle + " ======================");
for (int i = 0; i < pFiles.size(); i++) {
final File file = pFiles.get(i);
System.out.println(i + "\t" + file.getAbsolutePath());
}
System.out.println("============================================================");
}
/**
* this is the java.nio approach. this is NOT be a good solution for cases where you have to retrieve/handle files in your own code.
* this is only useful, if the any NIO class provides support. in this case, NIO class java.nio.file.Files helps handling local files.
* if NIO or your target system does not offer such helper methods, this way is harder to implement, as you have to set up the helper method yourself.
*/
static public Stream<Path> listFiles_newIO(final File pDir, final boolean pIncludeDirectories) throws IOException {
final Stream<Path> stream = Files.find(pDir.toPath(), 100,
(path, basicFileAttributes) -> {
final File file = path.toFile(); // conversion to File for easier access (f.e. isDirectory()), could also use NIO methods
return (pIncludeDirectories || !file.isDirectory() /* additional filters go here */ );
});
return stream;
}
static private void printDirs(final Stream<Path> pStream, final String pTitle) {
System.out.println("====================== " + pTitle + " ======================");
pStream.forEach(System.out::println);
System.out.println("============================================================");
}
}
AND, one must add, java.nio.file.Files.find() might be implemented recursively. But as it's just one call, this maybe could count as 'non-recursive' too.
ALSO, as the OP stated in comments, one might use Stack or other FIFO/LIFO collections. LIFO for a mixed depth-first, FIFO for breadth-first approach.

parallel file downloads using nio without creating threads per file download

I have tried a program which download files parallely using java.nio by creating a thread per file download.
package com.java.tftp.nio;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.DatagramChannel;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
/**
* This class is used to download files concurrently from tftp server by
* configuring the filenames, no of files.
*
* #author SHRIRAM
*
*/
public class TFTP_NIO_Client {
/**
* destination folder
* */
private String destinationFolder;
/**
* list of files names to download
* */
private List<String> fileNames;
/**
* integer indicates the number of files to download concurrently
* */
private int noOfFilesToDownload;
public TFTP_NIO_Client(List<String> fileNames, String destinationFolder,
int noOfFilesToDownload) {
this.destinationFolder = destinationFolder;
this.fileNames = fileNames;
this.noOfFilesToDownload = noOfFilesToDownload;
initializeHandlers();
}
/**
* This method creates threads to register the channel to process download
* files concurrently.
*
* #param noOfFilesToDownload
* - no of files to download
*/
private void initializeHandlers() {
for (int i = 0; i < noOfFilesToDownload; i++) {
try {
Selector aSelector = Selector.open();
SelectorHandler theSelectionHandler = new SelectorHandler(
aSelector, fileNames.get(i));
theSelectionHandler.start();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* Setup RRQ/WRQ packet Packet : | Opcode | FileName | 0 | mode | 0 |
* Filename -> Filename in array of bytes. 0 -> indicates end of file mode
* -> string in byte array 'netascii' or 'octet'
*
* #param aOpcode
* #param aMode
* #param aFileName
* #throws IOException
*/
private void sendRequest(int aOpcode, int aMode, String aFileName,
DatagramChannel aChannel, InetSocketAddress aAddress)
throws IOException {
// Read request packet
TFTPReadRequestPacket theRequestPacket = new TFTPReadRequestPacket();
aChannel.send(
theRequestPacket.constructReadRequestPacket(aFileName, aMode),
aAddress);
}
/**
* sends TFTP ACK Packet Packet : | opcode | Block# | opcode -> 4 -> 2 bytes
* Block -> block number -> 2bytes
*
* #param aBlock
*/
private ByteBuffer sendAckPacket(int aBlockNumber) {
// acknowledge packet
TFTPAckPacket theAckPacket = new TFTPAckPacket();
return theAckPacket.getTFTPAckPacket(aBlockNumber);
}
/**
* This class is used to handle concurrent downloads from the server.
*
* */
public class SelectorHandler extends Thread {
private Selector selector;
private String fileName;
/**
* flag to indicate the file completion.
* */
private boolean isFileReadFinished = false;
public SelectorHandler(Selector aSelector, String aFileName)
throws IOException {
this.selector = aSelector;
this.fileName = aFileName;
registerChannel();
}
private void registerChannel() throws IOException {
DatagramChannel theChannel = DatagramChannel.open();
theChannel.configureBlocking(false);
selector.wakeup();
theChannel.register(selector, SelectionKey.OP_READ);
sendRequest(Constants.OP_READ, Constants.ASCII_MODE, fileName,
theChannel, new InetSocketAddress(Constants.HOST,
Constants.TFTP_PORT));
}
#Override
public void run() {
process();
}
private void process() {
System.out.println("Download started for " + fileName + " ");
File theFile = new File(destinationFolder
+ fileName.substring(fileName.lastIndexOf("/")));
FileOutputStream theFout = null;
try {
theFout = new FileOutputStream(theFile);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
while (!isFileReadFinished) {
try {
if (selector.select() == 0) {
try {
// sleep 2sec was introduced because selector is
// thread safe but keys are not thread safe
Thread.sleep(2000);
} catch (InterruptedException e) {
continue;
}
continue;
}
Set<SelectionKey> theSet = selector.selectedKeys();
Iterator<SelectionKey> theSelectedKeys = theSet.iterator();
synchronized (theSelectedKeys) {
while (theSelectedKeys.hasNext()) {
SelectionKey theKey = theSelectedKeys.next();
theSelectedKeys.remove();
if (theKey.isReadable()) {
isFileReadFinished = read(theKey, theFout,
fileName);
if (!isFileReadFinished) {
theKey.interestOps(SelectionKey.OP_READ);
}
} else if (theKey.isWritable()) {
// there is no implementation for file write to
// server.
theKey.interestOps(SelectionKey.OP_READ);
}
}
}
} catch (IOException ie) {
ie.printStackTrace();
}
}
System.out.println("Download finished for " + fileName);
try {
if (selector.isOpen()) {
selector.close();
}
if (theFout != null) {
theFout.close();
}
} catch (IOException ie) {
}
}
}
/**
* #param aKey
* registered key for the selector
* #param aOutStream
* - file output stream to write the file contents.
* #return boolean
* #throws IOException
*/
private boolean read(SelectionKey aKey, OutputStream aOutStream,
String aFileName) throws IOException {
DatagramChannel theChannel = (DatagramChannel) aKey.channel();
// data packet
TFTPDataPacket theDataPacket = new TFTPDataPacket();
ByteBuffer theReceivedBuffer = theDataPacket.constructTFTPDataPacket();
SocketAddress theSocketAddress = theChannel.receive(theReceivedBuffer);
theReceivedBuffer.flip();
byte[] theBuffer = theReceivedBuffer.array();
byte[] theDataBuffer = theDataPacket.getDataBlock();
if (theDataPacket.getOpCode() == Constants.OP_DATA) {
int theLimit = theDataPacket.getLimit();
// checks the limit of the buffer because a packet with data less
// than 512 bytes of content signals that it is the last packet in
// transmission for this particular file
if (theLimit != Constants.MAX_BUFFER_SIZE
&& theLimit < Constants.MAX_BUFFER_SIZE) {
byte[] theLastBlock = new byte[theLimit];
System.arraycopy(theBuffer, 0, theLastBlock, 0, theLimit);
// writes the lastblock
aOutStream.write(theLastBlock);
// sends an acknowledgment to the server using TFTP packet
// block number
theChannel
.send(sendAckPacket((((theBuffer[2] & 0xff) << 8) | (theBuffer[3] & 0xff))),
theSocketAddress);
if (theChannel.isOpen()) {
theChannel.close();
}
return true;
} else {
aOutStream.write(theDataBuffer);
// sends an acknowledgment to the server using TFTP packet
// block number
theChannel
.send(sendAckPacket((((theBuffer[2] & 0xff) << 8) | (theBuffer[3] & 0xff))),
theSocketAddress);
return false;
}
} else if (Integer.valueOf(theBuffer[1]) == Constants.OP_ERROR) {
System.out.println("File : " + aFileName + " not found ");
handleError(theReceivedBuffer);
}
return false;
}
/**
* This method handles the error packet received from Server.
*
* #param aBuffer
*/
private void handleError(ByteBuffer aBuffer) {
// Error packet
new TFTPErrorPacket(aBuffer);
}
}
Is it possible to download multiple files in parallel using java.nio by not creating a thread per file download? If yes can anybody suggest a solution to proceed further.
I would provide an approach to achieve what you are aiming for :
Let L the list of files to be downloaded.
Create a Map M which will hold the mapping of File name to be downloaded and the corresponding Selector instance.
For each file F in L
Get Selector SK from M corresponding to F
Process the state of the Selector by checking for any of the events being ready.
If processing is complete then set the Selector corresponding to F as null. This will help in identifying files
whose
processing is completed.Alternatively, you can remove F from
L; so that the next time you are looping you only process files that are not yet completely downloaded.
The above being said, I am curious to understand why you would want to attempt such a feat? If the thought process behind this requirement is to reduce the number of threads to 1 then it is not correct. Remember, you would end up really taxing the single thread running and for sure your throughput would not necessarily be optimal since the single thread would be dealing with both network as well as disk I/O. Also, consider the case of encountering an exception while writing one of the several files to the disk - you would end up aborting the transfer for all the files; something I am sure you do not want.
A better and more scalable approach would be to poll selectors on a single thread, but hand off any I/O activity to a worker thread. A better approach still would be to read the techniques presented in Doug Lea's paper and implement them. In fact Netty library already implements this pattern and is widely used in production.

illegal start of type Java

I have a menu-driven program allows the user to add, remove and display the name of a person in a queue.
My program compiles and runs perfectly for me.
However, when my instructor tested it, he stated it won't compile and receives this error?:
QueueProgram.java:24: illegal start of type
MyQueue<String> queue= new MyQueue<>(15);
^
1 error
My code:
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import javax.swing.JOptionPane;
public class QueueProgram {
/**
* Driver code to test class
*
* #param arguments
* Commandline arguments not used
* #throws IOException
*/
public static void main(String[] arguments) throws IOException {
//Queue Object
MyQueue<String> queue= new MyQueue<>(15);
String name;
//reading file
read(queue,arguments[0]);
String[] array = { "Offer Person", "Poll Person", "Peek person",
"Display Queue", "Exit Program"};
int choice = 0;
// display loop
while (choice != array.length-1) {
choice = JOptionPane.showOptionDialog(null, // put in center of screen
"Press a Button", // message to user
"Queue(Line) of People", // title of window
JOptionPane.YES_NO_CANCEL_OPTION, // type of option
JOptionPane.QUESTION_MESSAGE, // type of message
null, // icon
array, // array of strings
array[array.length - 1]); // default choice (last one)
if(choice==0){
//inserting the new name in queue
name=JOptionPane.showInputDialog(null,"Enter Person's name","Input");
queue.offer(name);
}
else if(choice==1){
//Display and remove the name which is at front of line
JOptionPane.showMessageDialog(null, queue.poll() + " is next in line");
}
else if(choice==2){
//Display name which is at front of line
JOptionPane.showMessageDialog(null, queue.peek() + " is front of the line");
}
else if(choice==3){
//Dispay all the list
JOptionPane.showMessageDialog(null, queue.toString());
}
//JOptionPane.showMessageDialog(null, "Your pressed button #" + choice);
}
//calling writing function
write(queue, arguments[1]);
}// end of main()
/**
* Reads a file
* #param queue
* #param file_name name of file
*/
public static void read(QueueInterface<String> queue, String file_name) throws IOException{
String name;
//creating a buffer reader to read
BufferedReader br= new BufferedReader(new FileReader(file_name));
while((name=br.readLine()) != null){
//putting in the queue
queue.offer(name);
}
//closing buffer reader
br.close();
}
/**
* Writes to file
* #param queue QueueInterface methods
* #param file_name name of file
*/
public static void write(QueueInterface<String> queue, String file_name) throws IOException{
String name;
//creating a buffer writer to write
BufferedWriter bw= new BufferedWriter(new FileWriter(file_name));
while((name=queue.poll()) != null){
//writin in file
bw.write(name);
bw.newLine();
}
//closing buffer
bw.close();
}
}// end of class
class MyQueue<T> extends ArrayQueue<T>{
/**
* Constructor
*
* #param max is the greatest number of elements in the queue
*/
public MyQueue(int max) {
super(max);
}
/**
* Returns a string representation of the object
*
* #return a name on different lines
*/
public String toString() {
// create a variable
String element = "";
int count=frontIndex;
// check to see if not empty
if (!this.empty()) {
// get the address of front element
while(count<=endIndex){
element = element +(String) array[count]+"\n";
count++;
}
}
// return either null or element
return element;
}
}
Any idea what could cause this error?
The diamond operator (<>) was introduced in Java 7 only. You should specify the type argument (new MyQueue<String>(15)) explicitly and your instructor will be able to compile it.
You can find the explanation here - Java SE 7 Features and Enhancements - Type Inference for Generic Instance Creation.

How to restart java web start application programatically

I am trying to achieve the automatic update for my java web-start applicaiton. Logic: I am fetching the jnlp file from the server and comparing the timestamp with the current one. if there is difference then download the latest file and restart the application with javaws command. Now I have two problems.
1. I am not able to fetch the local jnlp file (because the location for jnlp file is different for different operating system as mentioned here
2. I am not able to find a graceful way to restart the application after killing the current running application. I would appreciate if there is any other graceful solution available. Thanks
My code:
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Date;
import javax.swing.JOptionPane;
import org.apache.log4j.Logger;
import com.ibm.nzna.projects.qit.app.PropertySystem;
/**
* QitUpdater . java
*
* #Info Checks for updates for the JWS Application
*
* This program will try to use a given URL to connect online to check for
* a newer version of the jnlpFile. The program then compares the last
* modified dates of the local copy and the URL copy, and notifies the
* user if the URL copy is newer, via a Dialog box popup. The newer
* version can then, be downloaded and installed using JWS.
*
* #Warnings If the server containing QIT changes its directory or location then
* the program will error out every time since the URL link is
* hard-coded.
*
* #author Ashish Tyagi
* #version 4/22/2013
*
*/
public abstract class QitUpdater {
// constant which hold the location of property file and server location
public final static String jnlpFile = PropertySystem.getString(PropertySystem.PROPERTY_JNLP_FILE);
public final static String fileDirectoryURL = PropertySystem.getString(PropertySystem.PROPERTY_JNLP_SERVER);
static Logger logger = Logger.getLogger(QitUpdater.class.getName());
// private variables
private static HttpURLConnection huc;
private static File oldFile = null;
private static long localDate = 0;
private static long remoteDate = 0;
public static void checkUpdates(boolean displayErrors,boolean displayIsUpToDate,boolean autoRunner){
logger.info("value of jnlp file: "+fileDirectoryURL+"/"+jnlpFile);
if(jnlpFile !=null && fileDirectoryURL != null)
checkForUpdates(displayErrors, displayIsUpToDate, autoRunner);
}
/**
* Starts a new task to check for QIT updates.
*
* If there is no connection or the program is up to date, then this method
* returns normally. Otherwise, it will exit the old program and execute the
* newer QIT program.
*
* #param displayErrors
* will show all error messages in popups, if any occur. This is
* recommended to be false on boostrapping, so the 'offline user'
* is not annoyed.
*
* #param displayIsUpToDate
* will show a popup letting the user know if their current
* version of QIT is up to date, if they have the most up to date
* version. Otherwise, it will ignore the popup.
*
* #param autoRunNewer
* will automatically execute the newer JNLP file, if it is found
* or downloaded. This is recommended to be false if this method
* is being called from within a user action, like a button
* press. If it is set to false, a dialog will ask the user to
* restart QIT in order to finish updating the program, if an
* update was found.
*/
public static void checkForUpdates(boolean displayErrors,
boolean displayIsUpToDate, boolean autoRunNewer) {
// Try to find a similarly named file
// in the current local directory
oldFile = new File(jnlpFile);
if (oldFile.exists()) // find the JNLP file
{
// grab the local last modified date to compare
localDate = oldFile.lastModified();
}
// Try to access the base URL
int code = 404;
try {
URL u = new URL(fileDirectoryURL);
huc = (HttpURLConnection) u.openConnection();
huc.setRequestMethod("GET");
huc.connect();
code = huc.getResponseCode();
} catch (MalformedURLException e) {
if (displayErrors)
printError("Error: Could not check for updates.\nMalformedURLException: "
+ e.getMessage()
+ "\n\nClick OK to continue using QIT.");
return;
} catch (IOException e) {
if (displayErrors)
printError("Error: Could not check for updates.\nIOException: "
+ e.getMessage()
+ "\n\nClick OK to continue using QIT.");
return;
}
if (code == 200) {
// 200 is a valid connection
// scan URL for versions
try {
StringBuilder sb = new StringBuilder();
sb.append(fileDirectoryURL).append("/").append(jnlpFile);
URL u = new URL(sb.toString());
huc = (HttpURLConnection) u.openConnection();
huc.setRequestMethod("GET");
huc.connect();
// grab the last modified date
remoteDate = huc.getLastModified();
} catch (MalformedURLException e) {
if (displayErrors)
printError("Error: Failed to download.\n\n"
+ e.getMessage()
+ "\n\nClick OK to continue using QIT.");
return;
} catch (IOException e) {
if (displayErrors)
printError("Error: Failed to download.\n\n"
+ e.getMessage()
+ "\n\nClick OK to continue using QIT.");
return;
}
// compare last modified dates of JNLP files
if (remoteDate != localDate) {
// found a newer version of JNLP
// ask to download
if (0 == printQuestion("An updated version of QIT is available.\n\nLast updated:\n"
+ new Date(remoteDate).toString()
+ "\n\nDo you want to download and install it?")) {
// download and install
try {
downloadUrlFile(jnlpFile, fileDirectoryURL + "/"
+ jnlpFile);
oldFile.setLastModified(remoteDate);
// set the date to the date on the server
if (autoRunNewer) {
// run the JNLP file
try {
Runtime.getRuntime()
.exec("javaws "+jnlpFile);
System.exit(0);// quit this version of QIT
} catch (IOException e) {
if (displayErrors)
printError("Error:\n"
+ e.getMessage()
+ "\n\nClick OK to continue using QIT.");
}
} else
printInfo("In order to finish installing the\nupdate, QIT needs to be restarted.");
} catch (Exception e) {
if (displayErrors)
printError("Error: Failed to download " + jnlpFile
+ ".\n\n" + e.getMessage()
+ "\n\nClick OK to continue using QIT.");
}
}
} else {
// modified dates are the same
// try to launch the current JNLP
if(oldFile.exists())
{
// run the JNLP file
try {
Runtime.getRuntime()
.exec("javaws "+jnlpFile);
System.exit(0);// quit this version of QIT
} catch (IOException e) {
if (displayErrors)
printError("Error:\n"
+ e.getMessage()
+ "\n\nClick OK to continue using QIT.");
}
}
// up to date
if (displayIsUpToDate)
printInfo("QIT is up to date.\n\nLast update was:\n"
+ (new Date(localDate).toString())
+ "\n\nClick OK to continue.");
}
}
return;
}
/**
* Downloads the urlString to the filename at the current directory.
*
* #param filename
* #param urlString
* #throws MalformedURLException
* #throws IOException
*/
public static void downloadUrlFile(String filename, String urlString)
throws MalformedURLException, IOException {
BufferedInputStream in = null;
FileOutputStream fout = null;
try {
URL url = new URL(urlString);
huc = (HttpURLConnection) url.openConnection();
in = new BufferedInputStream(url.openStream());
fout = new FileOutputStream(filename);
byte data[] = new byte[1024];
int count;
while ((count = in.read(data, 0, 1024)) != -1) {
fout.write(data, 0, count);
}
} finally {
if (in != null)
in.close();
if (fout != null)
fout.close();
}
}
/**
* Display an error
*
* #param e
*/
private static void printError(String e) {
JOptionPane.showMessageDialog(null, e, "Error",
JOptionPane.ERROR_MESSAGE);
}
/**
* Display some information
*
* #param s
*/
private static void printInfo(String s) {
JOptionPane.showMessageDialog(null, s, "Information",
JOptionPane.INFORMATION_MESSAGE);
}
/**
* Prompt a Yes/No Question
*
* #param String
* q
* #return int 0 means YES, 1 means NO, -1 means they clicked "X" close
*/
private static int printQuestion(String q) {
return JOptionPane.showConfirmDialog(null, q, "Question",
JOptionPane.YES_NO_OPTION);
}
}
I am trying to achieve the automatic update for my java web-start application
Use the DownloadService of the JNLP API.
DownloadService service allows an application to control how its own resources are cached, to determine which of its resources are currently cached, to force resources to be cached, and to remove resources from the cache. The JNLP Client is responsible for providing a specific implementation of this service.

How do you continuously read a file in Java?

I'm trying to figure out how to continuously read a file and once there is a new line added, output the line. I'm doing this using a sleep thread however it just seems to blow through the whole file and exit the program.
Any suggestions what I'm doing wrong?
Here is my code:
import java.io.*;
import java.lang.*;
import java.util.*;
class jtail {
public static void main (String args[])
throws InterruptedException, IOException{
BufferedReader br = new BufferedReader(
new FileReader("\\\\server01\\data\\CommissionPlanLog.txt"));
String line = null;
while (br.nextLine ) {
line = br.readLine();
if (line == null) {
//wait until there is more of the file for us to read
Thread.sleep(1000);
}
else {
System.out.println(line);
}
}
} //end main
} //end class jtail
thanks in advance
UPDATE: I've since changed the line "while (br.nextLine ) {" to just "while (TRUE) {"
This in somewhat old, but I have used the mechanism and it works pretty well.
edit: link no longer works, but I found it in the internet archive
https://web.archive.org/web/20160510001134/http://www.informit.com/guides/content.aspx?g=java&seqNum=226
The trick is to use a java.io.RandomAccessFile, and periodically check if the file length is greater that your current file position. If it is, then you read the data. When you hit the length, you wait. wash, rinse, repeat.
I copied the code, just in case that new link stops working
package com.javasrc.tuning.agent.logfile;
import java.io.*;
import java.util.*;
/**
* A log file tailer is designed to monitor a log file and send notifications
* when new lines are added to the log file. This class has a notification
* strategy similar to a SAX parser: implement the LogFileTailerListener interface,
* create a LogFileTailer to tail your log file, add yourself as a listener, and
* start the LogFileTailer. It is your job to interpret the results, build meaningful
* sets of data, etc. This tailer simply fires notifications containing new log file lines,
* one at a time.
*/
public class LogFileTailer extends Thread
{
/**
* How frequently to check for file changes; defaults to 5 seconds
*/
private long sampleInterval = 5000;
/**
* The log file to tail
*/
private File logfile;
/**
* Defines whether the log file tailer should include the entire contents
* of the exising log file or tail from the end of the file when the tailer starts
*/
private boolean startAtBeginning = false;
/**
* Is the tailer currently tailing?
*/
private boolean tailing = false;
/**
* Set of listeners
*/
private Set listeners = new HashSet();
/**
* Creates a new log file tailer that tails an existing file and checks the file for
* updates every 5000ms
*/
public LogFileTailer( File file )
{
this.logfile = file;
}
/**
* Creates a new log file tailer
*
* #param file The file to tail
* #param sampleInterval How often to check for updates to the log file (default = 5000ms)
* #param startAtBeginning Should the tailer simply tail or should it process the entire
* file and continue tailing (true) or simply start tailing from the
* end of the file
*/
public LogFileTailer( File file, long sampleInterval, boolean startAtBeginning )
{
this.logfile = file;
this.sampleInterval = sampleInterval;
}
public void addLogFileTailerListener( LogFileTailerListener l )
{
this.listeners.add( l );
}
public void removeLogFileTailerListener( LogFileTailerListener l )
{
this.listeners.remove( l );
}
protected void fireNewLogFileLine( String line )
{
for( Iterator i=this.listeners.iterator(); i.hasNext(); )
{
LogFileTailerListener l = ( LogFileTailerListener )i.next();
l.newLogFileLine( line );
}
}
public void stopTailing()
{
this.tailing = false;
}
public void run()
{
// The file pointer keeps track of where we are in the file
long filePointer = 0;
// Determine start point
if( this.startAtBeginning )
{
filePointer = 0;
}
else
{
filePointer = this.logfile.length();
}
try
{
// Start tailing
this.tailing = true;
RandomAccessFile file = new RandomAccessFile( logfile, "r" );
while( this.tailing )
{
try
{
// Compare the length of the file to the file pointer
long fileLength = this.logfile.length();
if( fileLength < filePointer )
{
// Log file must have been rotated or deleted;
// reopen the file and reset the file pointer
file = new RandomAccessFile( logfile, "r" );
filePointer = 0;
}
if( fileLength > filePointer )
{
// There is data to read
file.seek( filePointer );
String line = file.readLine();
while( line != null )
{
this.fireNewLogFileLine( line );
line = file.readLine();
}
filePointer = file.getFilePointer();
}
// Sleep for the specified interval
sleep( this.sampleInterval );
}
catch( Exception e )
{
}
}
// Close the file that we are tailing
file.close();
}
catch( Exception e )
{
e.printStackTrace();
}
}
}
If you're planning to implement this on a reasonable sized application where multiple objects might be interested in processing the new lines coming to the file, you might want to consider the Observer pattern.
The object reading from the file will notify each object subscribed to it as soon as a line has been processed.
This will allow you to keep logic well separated on the class where it's needed.
Also consider org.apache.commons.io.input.Tailer if you do not have the requirement of writing this from scratch.
The way your code is written now, you will not go through your while loop when your 'line==null' because you are checking to see that it has a next line before you even get into the loop.
Instead, try doing a while(true){ } loop. That way, you will always be looping through it, catching your pause cases, until you hit a condition that would cause the program to end.

Categories

Resources