Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [aspectj-users] Saving information from several pointcuts to an ObjectOutputStream


Simone Gianni-2 wrote:
> 
> Ron is right,
> moreover, aspects are singletons by default, and can freely declare any 
> field (like private ObjectOutputStream stream = null) and then create 
> the stream only if it hasn't already been created.
> 
> Obviously, care should be taken to avoid threading issues (like more 
> threads creating the stream, writing to it etc..), but this is no way 
> different that how you would do it in plain java : using synchronized if 
> you don't have performance problems, using classes from the concurrent 
> package if you have strict performance requirements.
> 
> As in plain java, you'll not have any place to close the stream .. 
> finalizers are not reliable in java and we all have to live with that. 
> For a normal stream on a decent OS, flushing it should anyway be enough.
> 
> Something like this should work :
> 
> public aspect TraceToFile {
> 
>   private ObjectOutputStream stream = null;
> 
>   private ObjectOutputStream setupStream() {
>     if (stream != null) return stream;
>     stream = new ... // initialize stream
>     return stream;
>   }
> 
>   after() : apointuct() || anoptherpointuct() {
>     ObjectOutputStream stream = setupStream();
>     stream.write(.... // write here info to the stream
>     stream.flush(); // Always flush it, you have no option to close it.
>   }
> 
> }
> 
> Hope this helps clarify,
> 
> Simone
> 
> Ron DiFrango wrote:
>> This sounds like a Singleton pattern me.  You could create a different
>> class
>> that is a Singleton that holds the static reference to the
>> ObjectOutputStream.  Then just have your pointcut get the object
>> Singleton
>> instance and have it write the data to the file.
>>
>>
>> On 2/19/09 6:07 PM, "baumar" <mbjava@xxxxxxxxx> wrote:
>>
>>   
>>> Hi,
>>>
>>> I'm trying to save information from several pointcut. More precise: I
>>> try to
>>> catch method call that eventually will match some conditions, write it
>>> to a
>>> class called DetailedSignature implements Serializable and save that
>>> information in one place.
>>>
>>> As far as I - as aspectJ-newbie  - understood, I cannot open a file for
>>> the
>>> whole life of an aspect, as there is no constructor. Therefore I tried
>>> to
>>> use an ObjectOutputStream like this:
>>>
>>> pointcut methodWithReturnValue() : (execution(!void
>>> *.*(..))&&(!within(org.test.serversimulation.DetailedSignature))
>>> &&(!within(org.test.serversimulation.SignatureCracker))
>>> &&(!within(junit.*)));
>>>
>>>     after() returning(Object o) : methodWithReturnValue() {
>>>
>>> try {
>>> File testDataFile = new File("serializedTest.data");
>>>        FileOutputStream fout = new FileOutputStream(testDataFile,
>>> true);
>>>        ObjectOutputStream oos = new ObjectOutputStream(fout);
>>>        oos.writeObject(detailedSignature);
>>>        oos.flush();
>>>        oos.close();
>>>        
>>>        System.out.println("wrote detailedSignature
>>> "+detailedSignature.toString());
>>>        }
>>>     catch (Exception e) { e.printStackTrace(); }
>>> }
>>>
>>> The program runs fine and I actually can see the data I want in the
>>> file.
>>> But when I try to read it, I get the exception:
>>>
>>> java.io.StreamCorruptedException: invalid type code: AC
>>>
>>> Now I searched and found that this always seems to happen when trying to
>>> append to an ObjectOutputStream multiple times
>>> (http://forums.sun.com/thread.jspa?threadID=5177084)
>>>
>>> Now I'm a bit at a loss. I was thinking of trying to write kind of
>>> bruteforce the information to a text file and based on that read and
>>> recreate the object manually, but that is certainly not very flexible.
>>>
>>> Does anyone know a way how to keep the outputstream open and pass it to
>>> several matches of the pointcut?
>>> Or would you know a more elegant strategy of saving objects from within
>>> several matches of a pointcut?
>>>
>>> Thanks for help
>>>
>>> Mark
>>>
>>>
>>>
>>>
>>> (start of the file with captured method
>>>
>>> ¬í sr +org.test.serversimulation.DetailedSignature˜Ë±W„çÅ
>>> Z isLiteralValueL allmodifierst Ljava/lang/String;L
>>> callingobjectClassnameq ~ L callingobjectMethodNameq ~ L
>>> callingobjectPackageq ~ L callingobjectPackageAndClassq ~ L
>>> callingobjectParametersq ~ L  modifierst Ljava/util/ArrayList;L
>>> parametersq ~ L returnValueClassq ~ L returnValuePackageq ~ L
>>> returnValuePackageAndClassq ~ L signatureLongStringq ~
>>> ... all methods catched appear in the file.
>>>     
>>
>> Ron DiFrango       
>> Manager and Architect  |  CapTech Ventures
>> (804) 855-9196-6308  |  rdifrango@xxxxxxxxxxxxxxxxxxx
>>
>> _______________________________________________
>> aspectj-users mailing list
>> aspectj-users@xxxxxxxxxxx
>> https://dev.eclipse.org/mailman/listinfo/aspectj-users
>>   
> 
> 
> -- 
> Simone Gianni            CEO Semeru s.r.l.           Apache Committer
> http://www.simonegianni.it/
> 
> _______________________________________________
> aspectj-users mailing list
> aspectj-users@xxxxxxxxxxx
> https://dev.eclipse.org/mailman/listinfo/aspectj-users
> 
> 


Hi Ron and Simone,

Thanks for the fast help! I tried both ways and they work well. Eventually I
used the Singleton because it allows to set the file name for the stream
from the outside, closing it correctly from there and then pass the file
name to a reader.

Cheers

Markus
-- 
View this message in context: http://www.nabble.com/Saving-information-from-several-pointcuts-to-an-ObjectOutputStream-tp22111630p22138436.html
Sent from the AspectJ - users mailing list archive at Nabble.com.



Back to the top