forked from afimb/chouette
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'rutebanken_google_cloud_storage' into rutebanken_develop
# Conflicts: # mobi.chouette.common/src/main/java/mobi/chouette/common/PropertyNames.java # mobi.chouette.dao.iev/src/main/java/mobi/chouette/dao/iev/JobDAO.java # mobi.chouette.exchange/src/main/java/mobi/chouette/exchange/AbstractInputValidator.java # mobi.chouette.exchange/src/main/java/mobi/chouette/exchange/exporter/AbstractExporterCommand.java # mobi.chouette.model.iev/src/main/java/mobi/chouette/model/iev/Job.java # mobi.chouette.service/src/main/java/mobi/chouette/service/JobService.java # mobi.chouette.service/src/main/java/mobi/chouette/service/JobServiceManager.java # mobi.chouette.ws/src/main/java/mobi/chouette/ws/RestService.java
- Loading branch information
Showing
19 changed files
with
580 additions
and
99 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
mobi.chouette.common/src/main/java/mobi/chouette/common/file/FileServiceException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package mobi.chouette.common.file; | ||
|
||
/** | ||
* Exception thrown when file access fails. | ||
*/ | ||
public class FileServiceException extends RuntimeException { | ||
|
||
public FileServiceException() { | ||
} | ||
|
||
public FileServiceException(String message) { | ||
super(message); | ||
} | ||
|
||
public FileServiceException(String message, Throwable cause) { | ||
super(message, cause); | ||
} | ||
|
||
public FileServiceException(Throwable cause) { | ||
super(cause); | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
mobi.chouette.common/src/main/java/mobi/chouette/common/file/FileStore.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package mobi.chouette.common.file; | ||
|
||
import java.io.InputStream; | ||
import java.nio.file.Path; | ||
|
||
/** | ||
* Access of permanent files. | ||
*/ | ||
public interface FileStore { | ||
|
||
InputStream getFileContent(Path filePath); | ||
|
||
void writeFile(Path filePath, InputStream content); | ||
|
||
boolean delete(Path filePath); | ||
|
||
void deleteFolder(Path folder); | ||
|
||
void createFolder(Path folder); | ||
|
||
boolean exists(Path filePath); | ||
|
||
} |
60 changes: 60 additions & 0 deletions
60
mobi.chouette.common/src/main/java/mobi/chouette/common/file/FileStoreFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package mobi.chouette.common.file; | ||
|
||
import java.util.Set; | ||
|
||
import javax.enterprise.context.spi.CreationalContext; | ||
import javax.enterprise.inject.spi.Bean; | ||
import javax.enterprise.inject.spi.CDI; | ||
|
||
import lombok.extern.log4j.Log4j; | ||
import mobi.chouette.common.ContenerChecker; | ||
import mobi.chouette.common.PropertyNames; | ||
|
||
@Log4j | ||
public class FileStoreFactory { | ||
|
||
private static FileStore FILE_STORE; | ||
|
||
private static final Object LOCK = new Object(); | ||
|
||
public static final FileStore getFileStore() { | ||
if (FILE_STORE == null) { | ||
|
||
synchronized (LOCK) { | ||
if (FILE_STORE == null) { | ||
ContenerChecker contenerChecker = null; | ||
try { | ||
contenerChecker = CDI.current().select(ContenerChecker.class).get(); | ||
} catch (Exception e) { | ||
log.warn("Failed to access CDI, using default fileStore impl"); | ||
} | ||
|
||
if (contenerChecker != null) { | ||
String implBeanName = System.getProperty(contenerChecker.getContext() + PropertyNames.FILE_STORE_IMPLEMENTATION); | ||
if (implBeanName != null) { | ||
Set<Bean<?>> beans = CDI.current().getBeanManager().getBeans(implBeanName); | ||
|
||
if (beans.size() > 0) { | ||
Bean<FileStore> bean = (Bean<FileStore>) beans.iterator().next(); | ||
CreationalContext<FileStore> ctx = CDI.current().getBeanManager().createCreationalContext(bean); | ||
FILE_STORE = (FileStore) | ||
CDI.current().getBeanManager().getReference(bean, FileStore.class, ctx); | ||
} else { | ||
throw new IllegalArgumentException("FileStore implementation with bean name: " + implBeanName + " not found"); | ||
} | ||
} else { | ||
log.warn("No FileStore implementation defined, using LocalFileStore as default"); | ||
} | ||
|
||
} | ||
|
||
if (FILE_STORE == null) { | ||
FILE_STORE = new LocalFileStore(); | ||
} | ||
} | ||
} | ||
|
||
} | ||
return FILE_STORE; | ||
} | ||
} |
81 changes: 81 additions & 0 deletions
81
mobi.chouette.common/src/main/java/mobi/chouette/common/file/LocalFileStore.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package mobi.chouette.common.file; | ||
|
||
import java.io.File; | ||
import java.io.FileInputStream; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
|
||
import javax.ejb.Stateless; | ||
|
||
import lombok.extern.log4j.Log4j; | ||
|
||
import org.apache.commons.io.FileUtils; | ||
|
||
import static mobi.chouette.common.file.LocalFileStore.BEAN_NAME; | ||
|
||
/** | ||
* Store permanent files in local file system. | ||
*/ | ||
@Stateless(name = BEAN_NAME) | ||
@Log4j | ||
public class LocalFileStore implements FileStore { | ||
|
||
public static final String BEAN_NAME = "LocalFileStore"; | ||
|
||
@Override | ||
public InputStream getFileContent(Path filePath) { | ||
try { | ||
return new FileInputStream(filePath.toFile()); | ||
} catch (IOException ioE) { | ||
throw new FileServiceException("Failed to read from file: " + ioE.getMessage(), ioE); | ||
} | ||
} | ||
|
||
|
||
@Override | ||
public void writeFile(Path filePath, InputStream content) { | ||
try { | ||
FileUtils.copyInputStreamToFile(content, filePath.toFile()); | ||
} catch (IOException ioE) { | ||
throw new FileServiceException("Failed to write to file: " + ioE.getMessage(), ioE); | ||
} | ||
} | ||
|
||
@Override | ||
public boolean delete(Path filePath) { | ||
File file = filePath.toFile(); | ||
if (file.exists()) { | ||
return filePath.toFile().delete(); | ||
} | ||
return false; | ||
} | ||
|
||
@Override | ||
public void deleteFolder(Path folder) { | ||
if (Files.exists(folder)) { | ||
try { | ||
FileUtils.deleteDirectory(folder.toFile()); | ||
} catch (Exception e) { | ||
throw new FileServiceException("Failed to delete folder: " + folder, e); | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public boolean exists(Path filePath) { | ||
return Files.exists(filePath); | ||
} | ||
|
||
@Override | ||
public void createFolder(Path folder) { | ||
try { | ||
if (!Files.exists(folder)) { | ||
Files.createDirectories(folder); | ||
} | ||
} catch (IOException ioE) { | ||
throw new FileServiceException("Failed to create folder: " + ioE.getMessage(), ioE); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.