1 package org.apache.velocity.site.news;
2
3 /*
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
19 * under the License.
20 */
21
22 import org.apache.velocity.site.news.model.NewsManager;
23 import org.apache.velocity.site.plexus.PlexusKludgeException;
24 import org.apache.velocity.site.plexus.PlexusKludgeUtils;
25 import org.codehaus.plexus.PlexusContainer;
26 import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
27
28 /**
29 * Helper class to retrieve the NewsManager from the news plugin. Must not be in
30 * the model package or the class loaders will fail to find the right class.
31 */
32
33 public abstract class NewsManagerUtils
34 {
35
36 public static final String NEWS_CONTAINER_NAME = "NEWS_MANAGER";
37
38 private NewsManagerUtils()
39 {
40 }
41
42 public static NewsManager getNewsManager(final PlexusContainer container) throws VelocityNewsException
43 {
44
45 try
46 {
47 PlexusContainer newsPluginContainer = PlexusKludgeUtils.lookupContainer(container,
48 "org.apache.velocity.site:velocity-site-news-plugin");
49
50 return (NewsManager) newsPluginContainer.lookup(NewsManager.ROLE);
51 }
52 catch (ComponentLookupException cle)
53 {
54 throw new VelocityNewsException(cle);
55 }
56 catch (PlexusKludgeException pke)
57 {
58 throw new VelocityNewsException(pke);
59 }
60 }
61
62 /**
63 * This binds the site plugin to load all model classes from the news plugin
64 * class loader. In turn we can now look up the news plugin from the site
65 * plugin and retrieve the model to use it in the VelociMacro. This is a
66 * major kludge around the strange maven class hierarchy.
67 */
68 public static final void bindModelRealm(final PlexusContainer container) throws VelocityNewsException
69 {
70 try
71 {
72 PlexusContainer sitePluginContainer = PlexusKludgeUtils.lookupContainer(container,
73 "org.apache.maven.plugins:maven-site-plugin");
74 if (sitePluginContainer != null)
75 {
76 PlexusKludgeUtils.bindRealm(container, sitePluginContainer, "org.apache.velocity.site.news.model");
77 }
78 }
79 catch (PlexusKludgeException pke)
80 {
81 throw new VelocityNewsException(pke);
82 }
83 }
84 }