Create a folder inside a postgres testcontainer before init script - testcontainers

I have the following piece of code:
public static PostgreSQLContainer<?> postgreDBContainer = new PostgreSQLContainer<>("postgres:12")
.withInitScript("init-database-test.sql")
.withUsername("dba")
.withPassword("dba");
Inside the init script, I'm creating some tablespaces and associating the folders:
CREATE TABLESPACE tsd01 OWNER dba LOCATION '/tsd01';
CREATE TABLESPACE tsi01 OWNER dba LOCATION '/tsi01';
CREATE TABLESPACE tsisecurity01 OWNER dba LOCATION '/tsisecurity01';
These folders for the tablespaces should be created before the init script runs. How do I can able to do that?

I was able to solve this issue by extending the default PostgreSQLContainer and changing the containerIsStarted method:
public class CustomPostgreSQLContainer<SELF extends CustomPostgreSQLContainer<SELF>> extends PostgreSQLContainer<SELF> {
private static final Logger log = LoggerFactory.getLogger(CustomPostgreSQLContainer.class);
public CustomPostgreSQLContainer() {
super("postgres:12");
}
#Override
protected void containerIsStarted(InspectContainerResponse containerInfo) {
try {
log.debug("M=containerIsStarted, creating database namespace folders and setting permissions");
execInContainer("mkdir", "/tsd01");
execInContainer("chown", "-R", "postgres.postgres", "/tsd01/");
execInContainer("mkdir", "/tsi01");
execInContainer("chown", "-R", "postgres.postgres", "/tsi01/");
execInContainer("mkdir", "/tsisecurity01");
execInContainer("chown", "-R", "postgres.postgres", "/tsisecurity01/");
} catch (IOException | InterruptedException e) {
e.printStackTrace();
}
super.containerIsStarted(containerInfo);
}
}

Related

Eclipse RCP cmd prompt handler

I am developing and application whithin eclipse rcp framework (3.0). I would like to create an Eclipse RCP command inside of the run menu that sends shell commands to windows cmd prompt.
How can I trigger it with an event?
public class RunCmd extends AbstractHandler {
#Override
public Object execute(ExecutionEvent event) throws ExecutionException {
// I need to get runtime exec
}
}
Sample code for executing Windows Command prompt.
There are different methods available for checking process executed properly
public class SampleHandler extends AbstractHandler {
public static final String ID = "com.example.views.play";
#Override
public Object execute(ExecutionEvent event) throws ExecutionException {
String command = "adb shell input key 26";
try
{
Process process = Runtime.getRuntime().exec(command);
} catch (IOException e)
{
e.printStackTrace();
}
return null;
}
}

Spring Batch: FlatFileItemWriter to InputSteam/Byte array

So I've created a batch job which generates reports (csv files). I have been able to generate the the files seamlessly using FlatFileItemWriter but my end goal is to create an InputStream to call a rest service which will store the document or a byte array to store it in the database.
public class CustomWriter implements ItemWriter<Report> {
#Override
public void write(List<? extends Report> reports) throws Exception {
reports.forEach(this::writeDataToFile);
}
private void writeDataToFile(final Report data) throws Exception {
FlatFileItemWriter writer = new FlatFileItemWriter();
writer.setResource(new FileSystemResource("C:/reports/test-report.csv"));
writer.setLineAggregator(getLineAggregator();
writer.afterPropertiesSet();
writer.open(new ExecutionContext());
writer.write(data);
writer.close();
}
private DelimitedLineAggregator<Report> getLineAggregator(final Report report) {
DelimitedLineAggregator<Report> delimitedLineAgg = new DelimitedLineAggregator<Report>();
delimitedLineAgg.setDelimiter(",");
delimitedLineAgg.setFieldExtractor(getFieldExtractor());
return delimitedLineAgg;
}
private FieldExtractor<Report> getFieldExtractor() {
BeanWrapperFieldExtractor<Report> fieldExtractor = new BeanWrapperFieldExtractor<Report>();
fieldExtractor.setNames(COLUMN_HEADERS.toArray(new String[0]));
return fieldExtractor;
}
}
One way I could do this is to store the file locally temporarily and create a new step to pick the generated files up and do the sending/storing but I would really like to skip this step and send/store it in the first step.
How do I go about doing this?

Weblogic,EJB, $Proxy99 class cast exception

Following are the modules in my project,
1. EJB module (version 3): We prepare ejb jar of this module and deploy on Weblogic11g server. It deals with database operation. It has #local, #Remote interface and #stateless classes implementing #local,#Remote interfaces.
2. Web Application : This web application takes inputs (user uploads file) from users, validates file and inserts data into database. It uses RMI.
Problem: On production (weblogic 11g server ) sometimes we observe exception saying $Proxy99 cannot be cast to "Remote interface name" (for different different classes) e.g com.xyz.fileProcessSetting.FileProcessSttgFacadeRemote.
But after some time when we again upload file, it gets uploaded successfully without any error.
Now, I do not understand how come these remote objects becomes temporarily unavailable? Never faced this issue on development/UAT environment. Also no idea how to reproduce and fix it.
Please help. Thanks in advance.
#Remote
public interface FileProcessSttgFacadeRemote {
//methods
}
#Local
public interface FileProcessSttgFacadeLocal {
//methods
}
#Stateless
public class FileProcessSttgFacade implements FileProcessSttgFacadeLocal, FileProcessSttgFacadeRemote {
//methods
}
in weblogic-ejb-jar.xml
<weblogic-enterprise-bean>
<ejb-name>FileProcessSttgFacade</ejb-name>
<stateless-session-descriptor>
<business-interface-jndi-name-map>
<business-remote>com.xyz.fileProcessSetting.FileProcessSttgFacadeRemote</business-remote>
<jndi-name>FileProcessSttgFacade</jndi-name>
</business-interface-jndi-name-map>
</stateless-session-descriptor>
</weblogic-enterprise-bean>
In web application also in ejb module whenever we want to call methods we use following lookup method to get remote object:
public class someclass extends EjbLocator {
public void someMethod(){
FileProcessSttgFacadeRemote fpfr = (FileProcessSttgFacadeRemote) getService("FileProcessSttgFacade");
//other code
}
}
Following is the class used for JNDI lookup:
public class EjbLocator {
public Object getService(final String jndiName) throws Exception {
try {
obj = getDefaultContext().lookup(jndiName);
} catch (final Exception exp) {
exp.printStackTrace();
}
return obj;
}
protected Context getDefaultContext() {
try {
final Hashtable<String, String> env = new Hashtable<String, String>();
env.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory");
env.put(Context.SECURITY_PRINCIPAL,"weblogic");
env.put(Context.SECURITY_CREDENTIALS, "password");
env.put(Context.PROVIDER_URL, "t3://<ip>:<port>");
defaultContext = new InitialContext(env);
return defaultContext;
} catch (final NamingException nExp) {
nExp.printStackTrace();
}
return null;
}
}

org.dbunit.dataset.NoSuchTableException: While loading dataset to view

I'm looking forward to integrate dbUnit to a project. The project has Spring and has no ORM. While loading the XML dataSet to the db i'm getting
org.dbunit.dataset.NoSuchTableException: XXX_VW
"XXX_VW" is a db view. However, I'm able to load the dataset to any table. I've confirmed in db the required view exits and the metadata is similiar.
Below is the code I execute during setup method of my test.
DataSource dc = (MCDataSource) context.getBean("dataSource");
databaseTester = new DataSourceDatabaseTester(dc, dc.getUsername());
DatabaseConfig config = databaseTester.getConnection().getConfig();
config.setProperty(DatabaseConfig.PROPERTY_TABLE_TYPE, new String[]{"TABLE", "VIEW"});
databaseTester.setDataSet(this.getDataSet());
databaseTester.setTearDownOperation(DatabaseOperation.DELETE_ALL);
databaseTester.onSetup();
Any idea what could be the issue ?
Instead implement and setOperationListener and do setProperty during lifecycle methods of OperationListener.
databaseTester.setOperationListener(new IOperationListener() {
#Override
public void operationTearDownFinished(IDatabaseConnection connection) {
// TODO Auto-generated method stub
}
#Override
public void operationSetUpFinished(IDatabaseConnection connection) {
// TODO Auto-generated method stub
}
#Override
public void connectionRetrieved(IDatabaseConnection connection) {
DatabaseConfig config = connection.getConfig();
config.setProperty(DatabaseConfig.PROPERTY_TABLE_TYPE, new String[]{"TABLE", "VIEW"});
databaseTester.setDataSet(this.getDataSet());
databaseTester.setTearDownOperation(DatabaseOperation.DELETE_ALL);
databaseTester.onSetup();

How many newWatchService can I create in Java 7?

How many newWatchService can I make ?
try{
for(Path path : PathList) {
watcher = path.getFileSystem().newWatchService();
} catch (IOException e) {
log.error(e);
}
}
--> result: IOExeption: too many open files...
I think you are supposed to create only one watcher service, but register [m]any paths to it.
As per the example given by Oracle docs (https://docs.oracle.com/javase/tutorial/essential/io/walk.html), there is only one watch service created, as a member variable of WatchDir class. Note the "this.watcher"
public class WatchDir {
private final WatchService watcher;
elsewhere in the class...
/**
* Creates a WatchService and registers the given directory
*/
WatchDir(Path dir, boolean recursive) throws IOException {
this.watcher = FileSystems.getDefault().newWatchService();
The same service is used for registering all paths inside a given folder recursively.
Finally, the registration happens here...
/**
* Register the given directory with the WatchService
*/
private void register(Path dir) throws IOException {
WatchKey key = dir.register(watcher, ENTRY_CREATE, ENTRY_DELETE, ENTRY_MODIFY);

Resources