120 likes | 211 Views
Creating File Access Services. Presented by Ashraf Memon Hands-on Ghulam Memon, Longjiang Ding. Overview. Writing file access service classes in Java Generating service Deploying services with Apache Axis Generating client files and testing them.
E N D
Creating File Access Services Presented by Ashraf Memon Hands-on Ghulam Memon, Longjiang Ding
Overview • Writing file access service classes in Java • Generating service • Deploying services with Apache Axis • Generating client files and testing them
Writing file access service classes in Java • A spatial ASCII file will be used as an example for this service. • ASCII file format is not standard. • Sample ASCII file looks like Magnitude Date Time Latitude Longitude Depth 1.4 2005/07/07 11:15:57 37.534N 118.839W 4.1 1.9 2005/07/07 11:11:26 35.663N 121.073W 6.0 2.0 2005/07/07 10:58:00 36.102N 115.591W 0.0 1.9 2005/07/07 10:55:57 34.408N 119.386W 12.2
Writing file access service classes in Java (contd) • First line of the file contains labels i.e. coordinates and attributes • Each following line contains values • Each value is separated by space • Each record is separated by a new line.
Writing file access service classes in Java (contd) • Sample File Access Class contains 1 function which reads lat/lon values and attributes from quakes.dat file and return XML for those values • Function signature ispublic String getData(double magnitude) • Explanatory source code follows on next slide. • For complete source code check c:\data\csig05\ws\solutions\ascii\AsciiAccess.java
Writing file access service classes in Java (contd) • public String getData(double magnitude) throws IOException{ • String xml = "<table>\r\n\t"; • RandomAccessFile reader = new RandomAccessFile("C:\\data\\csig05\\ws\\solutions\\ascii\\data\\quakes.dat", "r"); • String line = reader.readLine(); • line = reader.readLine(); • while(line!=null){ • StringTokenizer tokenizer = new StringTokenizer(line, " "); • String[] tempArr = new String[tokenizer.countTokens()]; • for(int i=0;tokenizer.hasMoreTokens();i++){ • tempArr[i]=tokenizer.nextToken(); • } • if(Double.parseDouble(tempArr[0])>magnitude){ • xml+="<record>\r\n\t\t"; • xml+="<lon>"+tempArr[4]+"</lon>\r\n\t\t"; • xml+="<lat>"+tempArr[3]+"</lat>\r\n\t\t"; • xml+="<magnitude>"+tempArr[0]+"</magnitude>\r\n\t"; • xml+="</record>\r\n\t"; • } • line = reader.readLine(); • } • xml+="\r\n"; • xml+="</table>"; • return xml; • }
Testing the code • Navigate to solutions directory c:\data\csig05\ws\solutions • Open command prompt by right clicking on the ascii directory and selecting “Command Prompt Here” • Change to ascii directory by typing following at the command prompt cd ascii • Compile AsciiAccess.java file by typing following at command prompt javac AsciiAccess.java • Run program by typing following at command prompt java AsciiAccess • Output should be of the form: continued on the next page
Testing the code (contd) <table> <record> <lon>121.265W</lon> <lat>36.650N</lat> <magnitude>3.0</magnitude> </record> <record> <lon>115.771W</lon> <lat>32.042N</lat> <magnitude>2.6</magnitude> </record> <record> <lon>115.754W</lon> <lat>32.003N</lat> <magnitude>3.3</magnitude> </record> <record> <lon>120.530W</lon> <lat>35.974N</lat> <magnitude>2.8</magnitude> </record> <record> <lon>121.685W</lon> <lat>37.316N</lat> <magnitude>2.9</magnitude> </record> </table> • Type exit on the command prompt to quit
Deploying your code as a web services with Apache Axis • Copy generated class file to C:\tools\tomcat4.1\webapps\axis\WEB-INF\classes\ • Open using any text editor deployAsciiAccess.wsdd from C:\tools\tomcat4.1\webapps\axis\WEB-INF\classes\ • Verify the content so that it matches the current service and its corresponding class
Deploying services with Apache Axis • Navigate to C:\tools\tomcat4.1\webapps\axis\WEB-INF\ • Open command prompt by right clicking on the classes directory and selecting “Command Prompt Here” • Change to classes directory by typing following at the command prompt cd classes • Set classpath by typing classpath.bat on the command prompt • Execute deployment descriptor by typing deploy.bat deployAsciiAccess.wsdd at command prompt • This will deploy database webservice on Axis SOAP Server • Test it by going to http://localhost/axis/ and navigating to AsciiAccessService
Generating client files and testing them • Change directory to c:\data\csig05\ws\solutions\ascii by typing “cd c:\data\csig05\ws\solutions\ascii” on the command prompt • Compile AsciiAccessServiceClient.javaby typing following at command prompt • javac AsciiAccessServiceClient.java • Execute Client by typing following at command prompt • java AsciiAccessServiceClient • Output should be similar to previous one
Next Chapter • Creating Web Service to Access Database