You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@cocoon.apache.org by Christoph Hermann <ch...@guschtel.de> on 2005/08/01 14:09:45 UTC

Instantiating a class on Cocoon startup

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hello,

is there a way in Cocoon to load a class on cocoon startup?
What i want to accomplish is like read some sort of configuration file
and then hold these values in memory while cocoon is running.
So i thought of building a xml file which is loaded through xmlbeans
into a static var or so. Thath way i wouldn't have to load the
configuration File on each request.
Can anyone give me some hints on how to accomplish this?

With kind regards
Christoph Hermann
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.1 (MingW32)
Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org

iQIVAwUBQu4RCbHbLRxXJIjpAQIrDA/9E6yMZs+ZzqLIAaw5nDiyxKikUqnIcgXg
0Vq9+hMIso5ytV59YzDxhe/jABTvj4WpxhjLQFv37Oz4pEmZhan57NOTgVvTday3
L11fRLJ4q7HL1akk7T0ClDgp+HS6M8sR5ouZJjpPqPPW4trEaYlsb+38s7eR8MXi
de59hptVGdVfIpmS7mstz+IvZxbT/Yar6t1kimPcYJvQ5uhO9aA3FOdb9CUzA8bv
kpDa53hZFnJaURgmFHCGjd0yU1BzaCVUtOkkxiPwWNzSAbwr8wXsPNvf82SLwxmH
eEy0uaH2d1RDZVHwvnAuHD9/IiBhvGossxs1DD8V+nHSUDMXnI6JwMjEt5NMVKro
h6H48RIAOEoP4UGi/8WwSmZC6x2029f04Utxyn98ow+5wulr3cn+TfDKu2Ffbvjr
DnYpUJFPX36dsH3CpePlN7DVYE32SzlypR7Y5oXOANJkkvpkNu4zvfgz8qcc4Vrr
6NpeuCaBy1NbtzyS/y2Nvn2BAEq5tdK+Qk2miJu3wbaDNLvNudqDaERkRbhOBW08
JvJMaycyTI/AK8dsECy4rCSHvvAfczHRmOt9tiQ4Hqhg7D/wD7eMJbu7D7xDEDn5
8JEjF/nRUislJKeK4NrOO4KNECx5LEaNGdxJnTYpX/Vuv35rD3RcmQxsbp5Rmuyq
zE5Q40eBXsI=
=Fxd6
-----END PGP SIGNATURE-----

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@cocoon.apache.org
For additional commands, e-mail: users-help@cocoon.apache.org


Re: Instantiating a class on Cocoon startup

Posted by Bertrand Delacretaz <bd...@apache.org>.
Le 1 août 05, à 15:05, Christoph Hermann a écrit :
>> ...*BUT*, maybe you just need XMLFileModule[1]? ;-)
>> Or at least you should implement your own input module to easy obtain
>> your configuration.
>
> well, it looks very interesting. Is it documented somewhere in a more
> detailed way? i.e. how to access it from flowscript?

Here's an example which gets a value from an input module named "xyz"

function conf_get_nxms_value(path) {
     var module = null;
     var result = null;
     try {
         module  = cocoon.getComponent(InputModule.ROLE + 
"Selector").select("xyz");
         result = module.getAttribute(path,null,null);
         if(result==null) throw new java.lang.Exception("nxms 
configuration value '" + path + "' not found");
     } finally {
         // selected components need not be released, right?
     }
    return result;
}

This is perfect for reading configs from an XML file, with automatic 
reloading if the input module is configured for this.

-Bertrand

Re: Instantiating a class on Cocoon startup

Posted by Christoph Hermann <ch...@guschtel.de>.
g[R]eK schrieb:

Hello,

> If you want your class to be loaded at startup you have to create
> interface like:
> package your.package;
> 
> import org.apache.avalon.framework.component.Component;
> [...]

Thanks for the detailed explanation!

> *BUT*, maybe you just need XMLFileModule[1]? ;-)
> Or at least you should implement your own input module to easy obtain
> your configuration.

well, it looks very interesting. Is it documented somewhere in a more
detailed way? i.e. how to access it from flowscript?

The only docs i found were:
http://wiki.apache.org/cocoon/InputModules
and the link you told me.

Christoph

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@cocoon.apache.org
For additional commands, e-mail: users-help@cocoon.apache.org


Re: Instantiating a class on Cocoon startup

Posted by "g[R]eK" <gr...@bluebottle.com>.
Christoph Hermann napisał(a):

>-----BEGIN PGP SIGNED MESSAGE-----
>Hash: SHA1
>
>Hello,
>
>is there a way in Cocoon to load a class on cocoon startup?
>What i want to accomplish is like read some sort of configuration file
>and then hold these values in memory while cocoon is running.
>So i thought of building a xml file which is loaded through xmlbeans
>into a static var or so. Thath way i wouldn't have to load the
>configuration File on each request.
>Can anyone give me some hints on how to accomplish this?
>  
>
If you want your class to be loaded at startup you have to create 
interface like:
package your.package;

import org.apache.avalon.framework.component.Component;

public interface YourNiceIterface extends Component {
String ROLE = PersistenceFactory.class.getName();

//some methods
}

and then class:
public class YourNiceClass implements YourNiceInterface, Configurable, 
Serviceable, Initializable, Disposable, ThreadSafe {
//implement what you want
}

All these interfaces (excluding YourNiceInterface) are Avalon's 
interfaces and they are to tell that you need initialization 
(Initializable) of your component.
If you need some logging, then your class should derive from 
AbstractLogEnabled. If you need more details about intefaces, just ask.

Then you have to put in cocon.xconf something like this:
<component class="your.package.YourNiceClass" 
role="your.package.YourNiceInteface"/>

*BUT*, maybe you just need XMLFileModule[1]? ;-)
Or at least you should implement your own input module to easy obtain 
your configuration.

[1] 
http://cocoon.apache.org/2.1/apidocs/org/apache/cocoon/components/modules/input/XMLFileModule.html

-- 
g[R]eK

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@cocoon.apache.org
For additional commands, e-mail: users-help@cocoon.apache.org


AW: Instantiating a class on Cocoon startup

Posted by Jens Maukisch <jm...@s-und-n.de>.
Hi,

> 
> is there a way in Cocoon to load a class on cocoon startup? 
> What i want to accomplish is like read some sort of 
> configuration file and then hold these values in memory while 
> cocoon is running. So i thought of building a xml file which 
> is loaded through xmlbeans into a static var or so. Thath way 
> i wouldn't have to load the configuration File on each 
> request. Can anyone give me some hints on how to accomplish this?
> 

You can wirte an avalon component:

e.g.:
class ConfigManager implements Component, ThreadSafe, Initializable {

	String xmlConfig;

	public static String ROLE = ConfigManager.getClass().getName();

	public void initialize() {
		xmlConfig = ....;
	}

	public String getConfiguration() {
		return xmlConfig;
	}

}

and then you can get this component via the ServiceManager
e.g. in Flowscript:
var config = cocoon.getComponent(Packages.ConfigManager.ROLE);
print(config.getConfiguration());

hth
Jens


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@cocoon.apache.org
For additional commands, e-mail: users-help@cocoon.apache.org