Your post helped me - but in unexpected ways. :D
I just added this line:
private static final String PATH = "c:/primes.txt";
and used PATH instead getResourceAsStream() to my existing code and it worked. I guess it was a Java PATH issue. Thanks!
On 7/12/06, Rodrigo Ruiz <
[EMAIL PROTECTED]> wrote:
Ok, I see some mistakes in the code. My comments inline...
M S wrote:
> Hi,
>
> Well this is my Web Service file:
>
>
> import java.io.*;
>
> public class MyService2 {
>
> public String echo(String password) {
I guess you will fix this signature later ;-D
>
> int ctr = 1;
> FileInputStream fin;
> try {
> fin = (FileInputStream)
> getClass().getClassLoader().getResourceAsStream(" prime.txt");
ClassLoader.getResourceAsStream() searches within the classpath. This
means that the retrieved stream, if found, is read-only, and probably
within your WEB-INF/classes, WEB-INF/lib, or the .aar archive itself. In
any case, it will be read-only (it is an InputStream).
The ServletContext has a method to obtain an absolute path from a
context relative path. With that path you can create FileReaders and
FileWriters without problem. I know you can get an instance of this
class from any service/module, but I don't know how.
> BufferedReader br
> = new BufferedReader(new InputStreamReader(fin));
> ctr = Integer.parseInt(br.readLine());
> fin.close();
> } catch (Exception e) { System.out.println("prime.txt does
> not exist. Creating prime.txt...");}
>
>
> boolean primeReached = false;
> while (!primeReached) {
> ctr++;
> if (isPrime(ctr) && ctr!=4) {
> primeReached = true;
> break;
> }
> }
> PrintWriter fout;
> try {
> fout = new PrintWriter(new FileWriter("c:\\prime.txt"));
The file you are writing is not the same you were reading before.
> fout.println(String.valueOf(ctr));
> fout.close ();
> } catch (Exception e) {e.printStackTrace();}
>
>
> return Integer.valueOf(ctr).toString();
> }
Looking at your code I see that using a file for storage is not (in
general) a good idea. It will work if there is only one client. It will
probably fail very fast if you have several concurrent clients. So, if
you know there will be only one client, ignore this comment ;-P
try this modified version and tell me what happens:
private static final String PATH = "c:/primes.txt";
public int nextPrime() {
int last = 1;
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader(PATH));
last = Integer.parseInt(reader.readLine());
} catch (Exception e) {
e.printStackTrace();
} finally {
close(reader);
}
do {
last++;
} while (!isPrime(last));
PrintWriter writer = null;
try {
writer = new PrintWriter(new FileWriter(PATH));
writer.println(last);
} catch (Exception e) {
e.printStackTrace ();
} finally {
close(writer);
}
return last;
}
private void close(Object obj) {
try {
Method m = obj.getClass().getMethod("close", null);
m.invoke(obj, null);
} catch (Exception ignore) {
}
}
It may have some typos. I am writing the code directly on my mail
client, and I have no plugins for Java spelling :-D
Well, hope this helps you
Rodrigo Ruiz
>
> private boolean isPrime(int p) {
> int i;
> for(i=2;i<(p/2);i++) {
> if ( i*(p/i) == p ) return(false);
> }
> return(true);
> }
>
> }
>
> This is my services.xml:
> <service>
> <operation name="echo">
> <messageReceiver
> class="org.apache.axis2.rpc.receivers.RPCMessageReceiver "/>
> </operation>
> <parameter name="ServiceClass" locked="false">MyService2</parameter>
> </service>
>
> This is my WSDL:
> <wsdl:definitions targetNamespace=" http://ws.apache.org/axis2">
> -
> <wsdl:types>
> -
> <xs:schema targetNamespace="http:///xsd"
> elementFormDefault="qualified" attributeFormDefault="qualified">
> -
> <xs:element name="echo">
> -
> <xs:complexType>
> -
> <xs:sequence>
> <xs:element type="xs:string" name="param0"/>
> </xs:sequence>
> </xs:complexType>
> </xs:element>
> -
> <xs:element name="echoResponse">
> -
> <xs:complexType>
> -
> <xs:sequence>
> <xs:element type="xs:string" name="return"/>
> </xs:sequence>
> </xs:complexType>
> </xs:element>
> </xs:schema>
> </wsdl:types>
> -
> <wsdl:message name="echoMessage">
> <wsdl:part element="ns0:echo" name="part1"/>
> </wsdl:message>
> -
> <wsdl:message name="echoResponse">
> <wsdl:part element="ns0:echoResponse" name="part1"/>
> </wsdl:message>
> -
> <wsdl:portType name="MyService2PortType">
> -
> <wsdl:operation name="echo">
> <wsdl:input message="axis2:echoMessage"/>
> <wsdl:output message="axis2:echoResponse"/>
> </wsdl:operation>
> </wsdl:portType>
> -
> <wsdl:binding type="axis2:MyService2PortType"
> name="MyService2SOAP11Binding">
> <soap:binding style="document"
> transport="http://schemas.xmlsoap.org/soap/http"/>
> -
> <wsdl:operation name="echo">
> <soap:operation style="document" soapAction="urn:echo"/>
> -
> <wsdl:input>
> <soap:body use="literal"/>
> </wsdl:input>
> -
> <wsdl:output>
> <soap:body use="literal"/>
> </wsdl:output>
> </wsdl:operation>
> </wsdl:binding>
> -
> <wsdl:binding type="axis2:MyService2PortType"
> name="MyService2SOAP12Binding">
> <soap12:binding style="document"
> transport="http://schemas.xmlsoap.org/soap/http"/>
> -
> <wsdl:operation name="echo">
> <soap12:operation style="document" soapAction="urn:echo"/>
> -
> <wsdl:input>
> <soap12:body use="literal"/>
> </wsdl:input>
> -
> <wsdl:output>
> <soap12:body use="literal"/>
> </wsdl:output>
> </wsdl:operation>
> </wsdl:binding>
> -
> <wsdl:binding type="axis2:MyService2PortType"
> name="MyService2HttpBinding">
> <http:binding verb="POST"/>
> -
> <wsdl:operation name="echo">
> <http:operation location="echo"/>
> -
> <wsdl:input>
> <mime:content type="text/xml"/>
> </wsdl:input>
> -
> <wsdl:output>
> <mime:content type="text/xml"/>
> </wsdl:output>
> </wsdl:operation>
> </wsdl:binding>
> -
> <wsdl:service name="MyService2">
> -
> <wsdl:port binding="axis2:MyService2SOAP11Binding"
> name="MyService2SOAP11port_http">
> <soap:address location=" http://localhost:8080/axis2/services/MyService2"/>
> </wsdl:port>
> -
> <wsdl:port binding="axis2:MyService2SOAP12Binding"
> name="MyService2SOAP12port_http">
> <soap12:address location="http://localhost:8080/axis2/services/MyService2"/>
> </wsdl:port>
> -
> <wsdl:port binding="axis2:MyService2HttpBinding"
> name="MyService2Httpport0">
> <http:address location=" http://localhost:8080/axis2/rest/MyService2"/>
> </wsdl:port>
> </wsdl:service>
> </wsdl:definitions>
>
> And this is my client:
> public class Client {
>
> public static void main(String[] args) throws Exception {
>
> MyService2Stub stub = new MyService2Stub();
>
> //Create the request
> MyService2Stub.Echo request = new MyService2Stub.Echo();
> request.setParam0("3IsAnOddPrime");
>
> //Invoke the service
> EchoResponse response = stub.echo(request);
>
> System.out.println("Response : " + response.get_return());
> }
>
>
> I get "Response: 1" no matter what I do with the Web Service file for
> some reason.
>
>
> On 7/11/06, *Rodrigo Ruiz* <[EMAIL PROTECTED]
> <mailto:[EMAIL PROTECTED]>> wrote:
>
> Hi again, :-)
>
> Mmmmh, I haven't heard anything about this kind of problems with .aar
> files. I am afraid I don't use Axis2 myself, so my knowledge is
> limited :-P
>
> Where are you trying to write the file, and how do you get the path?
> Have you tried to write in a fixed absolute path? If you can't create a
> file, for example, at the user's home directory, it could be a
> permissions problem (either at file level, or at server security system
> level).
>
> AFAIK, .aar files can also be deployed as directories, but I will have
> to redirect you to the Axis documentation site (or to anyone else that
> could answer this), I'm sorry.
>
> Michael, there are several good articles at theserverside, and many
> implementations at a "google click" distance ;-)
>
> I would recommend you to start by looking at the ehcache project
> (http://ehcache.sourceforge.net/). It is pretty well documented.
>
> Regards,
> Rodrigo Ruiz
>
>
> Rodrigo Ruiz wrote:
> > Depending on the servlet container you are using, war archives may be
> > considered read-only. In this case you will not be able to write a
> file
> > within the application context.
> >
> > Some alternatives you have are:
> >
> > - Deploy your application as an "exploded war" (the exact name
> will vary
> > from container to container). When deploying in this mode, your
> classes
> > can write files at any location within your context.
> >
> > - Use an absolute path for your file. The path may be configured
> through
> > JNDI, or System properties, or you might put it into a subfolder
> of the
> > user home (this is very common in *nix environments).
> >
> >
> > Other options imply to use a different storage type:
> >
> > - Use the User Preferences API to store the value. This API is
> available
> > starting from Java 1.4.
> >
> > - Store it into the JNDI tree. This only works if the JNDI
> > implementation is writeable and persistent. For example, AFAIK, it
> will
> > not work in Tomcat
> >
> > - Use a DBMS. It may seem an overkill solution, but there are some
> very
> > lightweight databases there. They may be not appropriate for
> enterprise
> > solutions, but for a single value they are more than enough.
> Moreover,
> > it is possible that you already use one for another service.
> >
> > - Use a distributed cache. Another overkill solution, but you may be
> > already using it for another service, or find out other places
> where it
> > may be useful ;-)
> >
> > These options, although more complex to implement, bring you an extra
> > feature. They make your service "distributable", that is,
> deployable on
> > a cluster of redundant servers. With local files, each node in the
> > cluster would have its own "counter".
> >
> > Hope this helps,
> > Rodrigo Ruiz
> >
> > Michael McIntosh wrote:
> >> Your problem seems very similar to mine - It would be great if
> someone
> >> would point us to the documentation for the rules related to file
> access
> >> (read/write, path, etc.)
> >>
> >> Thanks,
> >> Mike
> >>
> >> "M S" < [EMAIL PROTECTED] <mailto:[EMAIL PROTECTED]>> wrote on
> 07/10/2006 10:40:47 AM:
> >>
> >>> Hi,
> >>>
> >>> I have a web service that is supposed to generate prime numbers. I
> >>> store the latest generated prime number in a file called prime.txt.
> >>> So for example, if the program is run and generated 3, 3 will be
> >>> stored in prime.txt and next time it will generate 5. If there is no
> >>> prime.txt , the program will generate 1 and try to create prime.txt.
> >>>
> >>> My problem is that my web service application does not seem to be
> >>> able to read/write a file. Any ideas on how this should be done?
> >>> Notice that the prime.txt must be able to saved for the duration of
> >>> the web service's lifetime.
> >>>
> >>> Regards
> >> ---------------------------------------------------------------------
> >> To unsubscribe, e-mail: [EMAIL PROTECTED]
> <mailto:[EMAIL PROTECTED] >
> >> For additional commands, e-mail: [EMAIL PROTECTED]
> <mailto:[EMAIL PROTECTED] >
> >>
> >>
> >>
> >
>
> --
> -------------------------------------------------------------------
> GRIDSYSTEMS Rodrigo Ruiz Aguayo
> Parc Bit - Son Espanyol
> 07120 Palma de Mallorca mailto:[EMAIL PROTECTED]
> <mailto: [EMAIL PROTECTED]>
> Baleares - Espa�a Tel:+34-971435085 Fax:+34-971435082
> http://www.gridsystems.com
> -------------------------------------------------------------------
>
>
> --
> No virus found in this outgoing message.
> Checked by AVG Free Edition.
> Version: 7.1.394 / Virus Database: 268.9.10/384 - Release Date:
> 10/07/2006
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> <mailto:[EMAIL PROTECTED] >
> For additional commands, e-mail: [EMAIL PROTECTED]
> <mailto:[EMAIL PROTECTED] >
>
>
>
> ------------------------------------------------------------------------
>
> No virus found in this incoming message.
> Checked by AVG Free Edition.
> Version: 7.1.394 / Virus Database: 268.9.10/384 - Release Date: 10/07/2006
--
-------------------------------------------------------------------
GRIDSYSTEMS Rodrigo Ruiz Aguayo
Parc Bit - Son Espanyol
07120 Palma de Mallorca mailto:[EMAIL PROTECTED]
Baleares - Espa�a Tel:+34-971435085 Fax:+34-971435082
http://www.gridsystems.com
-------------------------------------------------------------------
--
No virus found in this outgoing message.
Checked by AVG Free Edition.
Version: 7.1.394 / Virus Database: 268.9.10/385 - Release Date: 11/07/2006
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
