Package net.sf.mud.content.service

Source Code of net.sf.mud.content.service.ContentServiceImpl

/*
* Copyright 2006- the original author or authors.
*
* 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 net.sf.mud.content.service;

import java.io.InputStream;

import javax.jcr.Node;
import javax.jcr.Property;
import javax.jcr.Session;

import net.sf.mud.core.common.MudRuntimeException;

import org.apache.log4j.Logger;
import org.springmodules.jcr.support.JcrDaoSupport;

/**
* @author garsombke.franz
*/
public class ContentServiceImpl extends JcrDaoSupport implements ContentService {

  private static final Logger log = Logger.getLogger(ContentServiceImpl.class);

  public void persistContent(String absPath, String nodeName, InputStream stream) {
    try {
      Session session = getSession(true);
      Node root = session.getRootNode();
      Node workNode = root.getNode(absPath);
      Node node = workNode.addNode(nodeName);
      node.setProperty(nodeName, stream);
      // Store content
      session.save();
    } catch (Throwable e) {
      throw new MudRuntimeException(e);
    }
  }

  public InputStream getContent(String absPath, String nodeName) {
    InputStream stream = null;
    try {
      Session session = getSession(true);
      Node root = session.getRootNode();
      Node node = root.getNode(absPath);
      Node workNode = node.getNode(nodeName);
      Property property = workNode.getProperty(nodeName);
      stream = property.getStream();
    } catch (Throwable e) {
      throw new MudRuntimeException(e);
    }
    return stream;
  }

  public void removeContent(String absPath, String nodeName) {
    try {
      Session session = getSession(true);
      Node root = session.getRootNode();
      Node node = root.getNode(absPath);
      Node workNode = node.getNode(nodeName);
      workNode.remove();
      session.save();
    } catch (Throwable e) {
      throw new MudRuntimeException(e);
    }
  }

  public void init() throws Exception {
    // add the build and deploy nodes at startup
    Session session = getSession(true);
    Node root = session.getRootNode();
    Node node = root.addNode("mud");
    node.addNode("build");
    node.addNode("deploy");
    session.save();
  }

}
TOP

Related Classes of net.sf.mud.content.service.ContentServiceImpl

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.