001 /* 002 Licensed to the Apache Software Foundation (ASF) under one 003 or more contributor license agreements. See the NOTICE file 004 distributed with this work for additional information 005 regarding copyright ownership. The ASF licenses this file 006 to you under the Apache License, Version 2.0 (the 007 "License"); you may not use this file except in compliance 008 with the License. You may obtain a copy of the License at 009 010 http://www.apache.org/licenses/LICENSE-2.0 011 012 Unless required by applicable law or agreed to in writing, 013 software distributed under the License is distributed on an 014 "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 KIND, either express or implied. See the License for the 016 specific language governing permissions and limitations 017 under the License. 018 */ 019 package org.apache.wiki.providers; 020 021 import java.io.*; 022 import java.util.Properties; 023 024 import org.apache.log4j.Logger; 025 import org.apache.wiki.*; 026 import org.apache.wiki.api.exceptions.ProviderException; 027 028 /** 029 * Provides a simple directory based repository for Wiki pages. 030 * <P> 031 * All files have ".txt" appended to make life easier for those 032 * who insist on using Windows or other software which makes assumptions 033 * on the files contents based on its name. 034 * 035 */ 036 public class FileSystemProvider 037 extends AbstractFileProvider 038 { 039 private static final Logger log = Logger.getLogger(FileSystemProvider.class); 040 /** 041 * All metadata is stored in a file with this extension. 042 */ 043 public static final String PROP_EXT = ".properties"; 044 045 /** 046 * {@inheritDoc} 047 */ 048 public void putPageText( WikiPage page, String text ) 049 throws ProviderException 050 { 051 try 052 { 053 super.putPageText( page, text ); 054 putPageProperties( page ); 055 } 056 catch( IOException e ) 057 { 058 log.error( "Saving failed" ); 059 } 060 } 061 062 /** 063 * Stores basic metadata to a file. 064 */ 065 private void putPageProperties( WikiPage page ) 066 throws IOException 067 { 068 Properties props = new Properties(); 069 OutputStream out = null; 070 071 try 072 { 073 String author = page.getAuthor(); 074 String changenote = (String)page.getAttribute( WikiPage.CHANGENOTE ); 075 076 if( author != null ) 077 { 078 props.setProperty( "author", author ); 079 } 080 081 if( changenote != null ) 082 { 083 props.setProperty( "changenote", changenote ); 084 } 085 086 File file = new File( getPageDirectory(), 087 mangleName(page.getName())+PROP_EXT ); 088 089 out = new FileOutputStream( file ); 090 091 props.store( out, "JSPWiki page properties for page "+page.getName() ); 092 } 093 finally 094 { 095 if( out != null ) out.close(); 096 } 097 } 098 099 /** 100 * Gets basic metadata from file. 101 */ 102 private void getPageProperties( WikiPage page ) 103 throws IOException 104 { 105 Properties props = new Properties(); 106 InputStream in = null; 107 108 try 109 { 110 File file = new File( getPageDirectory(), 111 mangleName(page.getName())+PROP_EXT ); 112 113 if( file.exists() ) 114 { 115 in = new FileInputStream( file ); 116 117 props.load(in); 118 119 page.setAuthor( props.getProperty( "author" ) ); 120 121 String changenote = props.getProperty( "changenote" ); 122 if( changenote != null ) 123 { 124 page.setAttribute( WikiPage.CHANGENOTE, changenote ); 125 } 126 } 127 } 128 finally 129 { 130 if( in != null ) in.close(); 131 } 132 } 133 134 /** 135 * {@inheritDoc} 136 */ 137 public WikiPage getPageInfo( String page, int version ) 138 throws ProviderException 139 { 140 WikiPage p = super.getPageInfo( page, version ); 141 142 if( p != null ) 143 { 144 try 145 { 146 getPageProperties( p ); 147 } 148 catch( IOException e ) 149 { 150 log.error("Unable to read page properties", e ); 151 throw new ProviderException("Unable to read page properties, check logs."); 152 } 153 } 154 155 return p; 156 } 157 158 /** 159 * {@inheritDoc} 160 */ 161 public void deletePage(String pageName) throws ProviderException 162 { 163 super.deletePage(pageName); 164 165 File file = new File( getPageDirectory(), 166 mangleName(pageName)+PROP_EXT ); 167 168 if( file.exists() ) file.delete(); 169 } 170 171 /** 172 * {@inheritDoc} 173 */ 174 public void movePage( String from, 175 String to ) 176 throws ProviderException 177 { 178 File fromPage = findPage( from ); 179 File toPage = findPage( to ); 180 181 fromPage.renameTo( toPage ); 182 } 183 }