1 creating file access services presented by ashraf memon hands-on ashraf memon, ghulam memon

12
1 Creating File Access Services Presented by Ashraf Memon Hands-on Ashraf Memon, Ghulam Memon

Upload: phoebe-barton

Post on 30-Dec-2015

214 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: 1 Creating File Access Services Presented by Ashraf Memon Hands-on Ashraf Memon, Ghulam Memon

1

Creating File Access Services

Presented byAshraf Memon

Hands-onAshraf Memon, Ghulam Memon

Page 2: 1 Creating File Access Services Presented by Ashraf Memon Hands-on Ashraf Memon, Ghulam Memon

2

Overview

• Writing file access service classes in Java

• Generating service• Deploying services with Apache

Axis• Generating client files and testing

them

Page 3: 1 Creating File Access Services Presented by Ashraf Memon Hands-on Ashraf Memon, Ghulam Memon

3

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 Depth1.4 2005/07/07 11:15:57 37.534N 118.839W 4.11.9 2005/07/07 11:11:26 35.663N 121.073W 6.02.0 2005/07/07 10:58:00 36.102N 115.591W 0.01.9 2005/07/07 10:55:57 34.408N 119.386W 12.2

Page 4: 1 Creating File Access Services Presented by Ashraf Memon Hands-on Ashraf Memon, Ghulam Memon

4

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.

Page 5: 1 Creating File Access Services Presented by Ashraf Memon Hands-on Ashraf Memon, Ghulam Memon

5

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

Page 6: 1 Creating File Access Services Presented by Ashraf Memon Hands-on Ashraf Memon, Ghulam Memon

6

Writing file access service classes in Java (contd)

1. public String getData(double magnitude) throws IOException{2. String xml = "<table>\r\n\t";3. RandomAccessFile reader = new RandomAccessFile("C:\\data\\csig05\\ws\\

solutions\\ascii\\data\\quakes.dat", "r");4. String line = reader.readLine();5. line = reader.readLine();6. while(line!=null){7. StringTokenizer tokenizer = new StringTokenizer(line, " ");8. String[] tempArr = new String[tokenizer.countTokens()];9. for(int i=0;tokenizer.hasMoreTokens();i++){10. tempArr[i]=tokenizer.nextToken();11. }12. if(Double.parseDouble(tempArr[0])>magnitude){13. xml+="<record>\r\n\t\t";14. xml+="<lon>"+tempArr[4]+"</lon>\r\n\t\t";15. xml+="<lat>"+tempArr[3]+"</lat>\r\n\t\t";16. xml+="<magnitude>"+tempArr[0]+"</magnitude>\r\n\t";17. xml+="</record>\r\n\t";

18. }19. line = reader.readLine();20. }21. xml+="\r\n";22. xml+="</table>";23. return xml;24. }

Page 7: 1 Creating File Access Services Presented by Ashraf Memon Hands-on Ashraf Memon, Ghulam Memon

7

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

Page 8: 1 Creating File Access Services Presented by Ashraf Memon Hands-on Ashraf Memon, Ghulam Memon

8

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

Page 9: 1 Creating File Access Services Presented by Ashraf Memon Hands-on Ashraf Memon, Ghulam Memon

9

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

Page 10: 1 Creating File Access Services Presented by Ashraf Memon Hands-on Ashraf Memon, Ghulam Memon

10

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

Page 11: 1 Creating File Access Services Presented by Ashraf Memon Hands-on Ashraf Memon, Ghulam Memon

11

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.java by 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

Page 12: 1 Creating File Access Services Presented by Ashraf Memon Hands-on Ashraf Memon, Ghulam Memon

12

• Creating Web Service to Access Database

Next Chapter