1 63 64 package org.apache.jetspeed.services.cms.repository.slide; 65 66 import java.util.Hashtable ; 67 import java.util.Enumeration ; 68 import java.util.Date ; 69 import java.io.*; 70 import org.apache.slide.common.*; 71 import org.apache.slide.store.*; 72 import org.apache.slide.structure.*; 73 import org.apache.slide.content.*; 74 import org.apache.slide.util.logger.Logger; 75 76 import org.apache.jetspeed.services.resources.JetspeedResources; 77 78 85 public class CmsContentStore extends AbstractService 86 implements ContentStore { 87 88 89 91 92 public static final int BUFFER_SIZE = 2048; 93 public static final String CHARACTER_ENCODING = "8859_1"; 94 95 96 98 99 102 private String rootpath; 103 104 105 108 private boolean version = true; 109 110 115 private boolean removePriorContent = false; 116 117 120 private final String DEFAULT_ROOTPATH="."; 121 122 124 125 131 public synchronized void setParameters(Hashtable parameters) 132 throws ServiceParameterErrorException, 133 ServiceParameterMissingException { 134 rootpath = JetspeedResources.getString("services.CmsService.slide.content.rootpath"); 136 if (rootpath == null) { 137 rootpath = DEFAULT_ROOTPATH; 140 } 141 String versionValue = (String ) parameters.get("services.CmsService.slide.content.version"); 142 if (versionValue != null) { 143 version = Boolean.valueOf(versionValue).booleanValue(); 144 } 145 String removePriorContentValue = (String ) parameters.get("services.CmsService.slide.content.resetBeforeStarting"); 146 if (removePriorContentValue != null) { 147 removePriorContent = removePriorContentValue.equalsIgnoreCase("true"); 148 } 149 150 } 151 152 153 158 public synchronized void connect() 159 throws ServiceConnectionFailedException { 160 } 161 162 163 168 public synchronized void disconnect() 169 throws ServiceDisconnectionFailedException { 170 } 171 172 173 179 public synchronized void initialize(NamespaceAccessToken token) 180 throws ServiceInitializationFailedException { 181 try { 182 if ((removePriorContent) && (!DEFAULT_ROOTPATH.equals(rootpath))) 184 reset(); 185 File baseDir = new File(rootpath); 186 token.getLogger().log("FileContentStore rootpath: " + baseDir.getAbsolutePath() ,Logger.INFO); 187 baseDir.mkdirs(); 188 } catch (Exception e) { 189 e.printStackTrace(); 190 throw new ServiceInitializationFailedException(this, e); 191 } 192 } 193 194 195 200 public void reset() 201 throws ServiceResetFailedException { 202 String [] filter = new String [1]; 203 filter[0] = "*.*"; 204 new slidestore.reference.fileDeleter.DeleterWalker(rootpath, filter ); 205 } 206 207 208 214 public boolean isConnected() 215 throws ServiceAccessException { 216 return true; 217 } 218 219 220 225 public synchronized void commit() 226 throws ServiceAccessException { 227 } 228 229 230 232 233 239 public NodeRevisionContent retrieveRevisionContent 240 (Uri uri, NodeRevisionDescriptor revisionDescriptor) 241 throws ServiceAccessException, RevisionNotFoundException { 242 NodeRevisionContent result = null; 243 String revisionUri = null; 244 if (version) 245 revisionUri = uri.toString() + "_" 246 + revisionDescriptor.getRevisionNumber(); 247 else 248 revisionUri = uri.toString(); 249 try { 250 File file = new File(rootpath + revisionUri); 251 FileInputStream is = new FileInputStream(file); 252 InputStreamReader reader = new InputStreamReader 253 (is, CHARACTER_ENCODING); 254 result = new NodeRevisionContent(); 255 result.setContent(reader); 256 result.setContent(is); 257 } catch (FileNotFoundException e) { 258 throw new RevisionNotFoundException 259 (uri.toString(), 260 revisionDescriptor.getRevisionNumber()); 261 } catch (IOException e) { 262 e.printStackTrace(); 263 throw new ServiceAccessException(this, e.getMessage()); 264 } 265 return result; 266 267 } 268 269 270 277 public void createRevisionContent 278 (Uri uri, NodeRevisionDescriptor revisionDescriptor, 279 NodeRevisionContent revisionContent) 280 throws ServiceAccessException, RevisionAlreadyExistException { 281 String revisionUri = null; 282 if (version) 283 revisionUri = uri.toString() + "_" 284 + revisionDescriptor.getRevisionNumber(); 285 else 286 revisionUri = uri.toString(); 287 288 try { 289 File file = new File(rootpath + revisionUri); 290 File parentFile = new File(file.getParent()); 291 if ((parentFile != null) && (!parentFile.exists())) { 292 parentFile.mkdirs(); 293 } 294 295 boolean created = !file.exists(); 296 if (!created) { 297 throw new RevisionAlreadyExistException 298 (uri.toString(), revisionDescriptor.getRevisionNumber()); 299 } 300 301 InputStream is = revisionContent.streamContent(); 302 303 if (is != null) { 304 OutputStream os = new FileOutputStream(file); 305 byte[] buffer = new byte[BUFFER_SIZE]; 307 long position = 0; 308 long contentLength = revisionDescriptor.getContentLength(); 309 310 while (true) { 311 int nChar = is.read(buffer); 312 if (nChar == -1) { 313 break; 314 } 315 os.write(buffer, 0, nChar); 316 position = position + nChar; 317 } 318 os.close(); 319 is.close(); 320 321 if (contentLength != -1) { 322 if (position != contentLength) { 323 revisionDescriptor.setContentLength(position); 325 if (position < contentLength) { 326 throw new IOException("Not enough bytes read"); 328 } 329 if (position > contentLength) { 330 throw new IOException("Too many bytes read"); 332 } 333 } 335 } else { 336 revisionDescriptor.setContentLength(position); 337 } 338 339 } else { 340 } 341 342 } catch (IOException e) { 343 throw new ServiceAccessException(this, e.getMessage()); 344 } catch(RevisionAlreadyExistException e) { 345 throw e; } catch (Exception e) { 347 e.printStackTrace(); 348 throw new ServiceAccessException(this, e.getMessage()); 349 } 350 } 351 352 353 360 public void storeRevisionContent 361 (Uri uri, NodeRevisionDescriptor revisionDescriptor, 362 NodeRevisionContent revisionContent) 363 throws ServiceAccessException, RevisionNotFoundException { 364 String revisionUri = null; 365 if (version) 366 revisionUri = uri.toString() + "_" 367 + revisionDescriptor.getRevisionNumber(); 368 else 369 revisionUri = uri.toString(); 370 try { 371 File file = new File(rootpath + revisionUri); 372 373 InputStream is = revisionContent.streamContent(); 374 375 if (is != null) { 376 OutputStream os = null; 377 try { 378 os = new FileOutputStream(file); 379 } catch (FileNotFoundException ex) { 380 File parentFile = new File(file.getParent()); 382 if ((parentFile != null) && (!parentFile.exists())) { 383 parentFile.mkdirs(); 384 } 385 os = new FileOutputStream(file); 386 } 387 byte[] buffer = new byte[BUFFER_SIZE]; 389 long position = 0; 390 long contentLength = revisionDescriptor.getContentLength(); 391 392 while (true) { 393 int nChar = is.read(buffer); 394 if (nChar == -1) { 395 break; 396 } 397 os.write(buffer, 0, nChar); 398 position = position + nChar; 399 } 400 os.close(); 401 is.close(); 402 403 if (contentLength != -1) { 404 if (position != contentLength) { 405 if (position < contentLength) { 406 throw new IOException("Not enough bytes read"); 408 } 409 if (position > contentLength) { 410 throw new IOException("Too many bytes read"); 412 } 413 } 415 } else { 416 revisionDescriptor.setContentLength(position); 417 } 418 419 } else { 420 } 421 422 } catch (FileNotFoundException e) { 423 throw new RevisionNotFoundException 424 (uri.toString(), revisionDescriptor.getRevisionNumber()); 425 } catch (IOException e) { 426 e.printStackTrace(); 427 throw new ServiceAccessException(this, e.getMessage()); 428 } 429 } 430 431 432 438 public void removeRevisionContent(Uri uri, 439 NodeRevisionDescriptor revisionDescriptor) 440 throws ServiceAccessException { 441 String revisionUri = null; 442 if (version) 443 revisionUri = uri.toString() + "_" + revisionDescriptor.getRevisionNumber(); 444 else 445 revisionUri = uri.toString(); 446 447 try { 448 File file = new File(rootpath + revisionUri); 449 boolean deleted = file.delete(); 450 File parentFile = new File(file.getParent()); 451 if (parentFile != null) { 452 parentFile.delete(); 453 } 454 } catch (Exception e) { 455 e.printStackTrace(); 456 throw new ServiceAccessException(this, e.getMessage()); 457 } 458 } 459 460 } 461 | Popular Tags |