/*
* Copyright 2010 t_yano.
*
* 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.
* under the License.
*/
package jp.javelindev.wicket.parameter.mount;
import jp.javelindev.wicket.parameter.mount.impl.DefaultResourceMounter;
import jp.javelindev.wicket.parameter.mount.impl.DefaultPageMounter;
import java.io.BufferedInputStream;
import java.io.InputStream;
import java.util.MissingResourceException;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import jp.javelindev.wicket.parameter.CanNotMountException;
import jp.javelindev.wicket.parameter.PathAnnotationProcessor;
import jp.javelindev.wicket.parameter.bind.MountPath;
import jp.javelindev.wicket.parameter.bind.MountPoint;
import org.apache.wicket.Page;
import org.apache.wicket.Resource;
import org.apache.wicket.protocol.http.WebApplication;
/**
*
* @author t_yano
*/
public class MountUtil {
public void mountAll(WebApplication application) throws CanNotMountException {
try {
InputStream stream = null;
try {
stream = new BufferedInputStream(application.getClass().getResourceAsStream("/" + PathAnnotationProcessor.XML_FILE_NAME));
} catch (MissingResourceException ex) {
throw new IllegalStateException("/" + PathAnnotationProcessor.XML_FILE_NAME + " does not exist. Do you setup PathAnnotationProcessor onto your compiler setting?");
}
JAXBContext context = JAXBContext.newInstance(MountPoint.class);
Unmarshaller unmarshaller = context.createUnmarshaller();
Object unmarshalled = unmarshaller.unmarshal(stream);
if (unmarshalled == null || (unmarshalled instanceof MountPoint) == false) {
throw new IllegalStateException("Can not unmarshall " + "/" + PathAnnotationProcessor.XML_FILE_NAME + ".");
}
MountPoint mountPoint = (MountPoint) unmarshalled;
IResourceMounter resourceMounter = newResourceMounter(application);
IPageMounter pageMounter = newPageMounter(application);
for (MountPath path : mountPoint.getPathSet()) {
final String mountPath = path.getMountPath();
final Class<?> targetClass = Class.forName(path.getClassName());
if(Resource.class.isAssignableFrom(targetClass)) {
//mount as SharedResource
resourceMounter.mount(mountPath, targetClass.asSubclass(Resource.class));
} else if(Page.class.isAssignableFrom(targetClass)) {
//mount as Page
pageMounter.mount(mountPath, targetClass.asSubclass(Page.class), path.isHybrid());
} else {
throw new IllegalStateException("A mount target, which is not subclass of Page nor Resource, is found. You can use @Path annotation only for subclasses of Page or Resource.");
}
}
} catch (ClassNotFoundException ex) {
throw new CanNotMountException("Page Class not found.", ex);
} catch (JAXBException ex) {
throw new CanNotMountException("Can not parse mountpoint.xml correctly.", ex);
}
}
public IResourceMounter newResourceMounter(WebApplication application) {
return new DefaultResourceMounter(application);
}
public IPageMounter newPageMounter(WebApplication application) {
return new DefaultPageMounter(application);
}
}