mcconnell 2004/04/02 00:08:48
Added: merlin/platform/tutorials/context/plus .cvsignore README.TXT
project.xml
merlin/platform/tutorials/context/plus/conf block.xml
merlin/platform/tutorials/context/plus/src/java/tutorial
DemoContext.java DemoContextProvider.java
HelloComponent.java
Log:
Add tutorial demonstrating custom context injection.
Revision Changes Path
1.1 avalon/merlin/platform/tutorials/context/plus/.cvsignore
Index: .cvsignore
===================================================================
maven.log
velocity.log
build
target
1.1 avalon/merlin/platform/tutorials/context/plus/README.TXT
Index: README.TXT
===================================================================
Constructor Injection of a Custom Context.
------------------------------------------
This tutorial covers usage of context entries using
a constructor supplied custom context.
$ maven
$ merlin -execute target\classes
[INFO ] (tutorial.hello): working directory set to: [your-directory]\home\tutorial
\hello
1.1 avalon/merlin/platform/tutorials/context/plus/project.xml
Index: project.xml
===================================================================
<?xml version="1.0" encoding="UTF-8"?>
<project>
<extend>${basedir}/../../project.xml</extend>
<id>merlin-tutorial-context-plus</id>
<name>Merlin Context Standard Tutorial</name>
<package>tutorial</package>
<currentVersion>1.0</currentVersion>
<inceptionYear>2003</inceptionYear>
<shortDescription>Merlin Context Custom Injection Tutorial.</shortDescription>
<dependencies>
<dependency>
<groupId>avalon-framework</groupId>
<artifactId>avalon-framework-api</artifactId>
<version>4.1.5</version>
</dependency>
<dependency>
<groupId>avalon-framework</groupId>
<artifactId>avalon-framework-impl</artifactId>
<version>4.1.5</version>
</dependency>
</dependencies>
<build>
<sourceDirectory>${basedir}/src/java</sourceDirectory>
<resources>
<resource>
<directory>${basedir}/conf</directory>
<targetPath>BLOCK-INF</targetPath>
<includes>
<include>block.xml</include>
</includes>
</resource>
</resources>
<jars></jars>
</build>
</project>
1.1 avalon/merlin/platform/tutorials/context/plus/conf/block.xml
Index: block.xml
===================================================================
<container name="tutorial">
<classloader>
<classpath>
<repository>
<resource id="avalon-framework:avalon-framework-impl" version="4.1.5"/>
</repository>
</classpath>
</classloader>
<component name="hello" class="tutorial.HelloComponent" activation="startup">
<context class="tutorial.DemoContextProvider"/>
</component>
</container>
1.1
avalon/merlin/platform/tutorials/context/plus/src/java/tutorial/DemoContext.java
Index: DemoContext.java
===================================================================
/*
* Copyright 2004 Apache Software Foundation
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
* implied.
*
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package tutorial;
import java.io.File;
/**
* An example of an custom context interface.
*/
public interface DemoContext
{
/**
* Return the working directory.
* @return the directory
*/
File getWorkingDirectory();
}
1.1
avalon/merlin/platform/tutorials/context/plus/src/java/tutorial/DemoContextProvider.java
Index: DemoContextProvider.java
===================================================================
/*
* Copyright 2004 Apache Software Foundation
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
* implied.
*
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package tutorial;
import java.util.Map;
import java.io.File;
import org.apache.avalon.framework.context.Context;
import org.apache.avalon.framework.context.DefaultContext;
import org.apache.avalon.framework.context.ContextException;
/**
* A demonstration class that that we will instantiate via
* context directives within the component declaration.
*/
public class DemoContextProvider extends DefaultContext implements DemoContext
{
/**
* A custom context type implementation must provide
* the following constructor.
* @param entries a map of context entries
*/
public DemoContextProvider( Context context )
{
super( context );
}
/**
* Return the working directory.
* @return the directory
*/
public File getWorkingDirectory()
{
try
{
return (File) super.get( "urn:avalon:home" );
}
catch( ContextException ce )
{
// should not happen
throw new RuntimeException( ce.toString() );
}
}
}
1.1
avalon/merlin/platform/tutorials/context/plus/src/java/tutorial/HelloComponent.java
Index: HelloComponent.java
===================================================================
/*
* Copyright 2004 Apache Software Foundation
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
* implied.
*
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package tutorial;
import java.io.File;
import org.apache.avalon.framework.logger.Logger;
import org.apache.avalon.framework.context.Context;
import org.apache.avalon.framework.context.ContextException;
/**
* Component demonstrating access to standard context entries.
* @avalon.component name="demo" lifestyle="singleton"
*/
public class HelloComponent
{
//------------------------------------------------------
// immutable state
//------------------------------------------------------
private final Logger m_logger;
private final File m_home;
/**
* Creation of a new HelloComponent instance using a
* container supplied logging channel and custom context.
* The context supplied by the container holds the
* standard context entries requested under the avalon.entry
* tags. The advantages of this approach is the isolation
* of context entry casting and key into a seperate context
* helper class.
*
* @avalon.context type="tutorial.DemoContext"
* @avalon.entry key="urn:avalon:home" type="java.io.File"
*/
public HelloComponent( Logger logger, DemoContext context )
{
m_logger = logger;
m_home = context.getWorkingDirectory();
m_logger.info( "working directory set to: " + m_home );
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]