-
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.
most code for headless property and disallow startup when memory too low
- Loading branch information
1 parent
e1bb98c
commit d09e9c8
Showing
59 changed files
with
2,903 additions
and
116 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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
74 changes: 74 additions & 0 deletions
74
...in/java/com/github/eostermueller/snail4j/config/DefaultGenericConfigFileReaderWriter.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,74 @@ | ||
package com.github.eostermueller.snail4j.config; | ||
|
||
import java.io.FileInputStream; | ||
import java.io.IOException; | ||
import java.nio.ByteBuffer; | ||
import java.nio.MappedByteBuffer; | ||
import java.nio.channels.FileChannel; | ||
import java.nio.charset.Charset; | ||
import java.nio.file.Path; | ||
import java.nio.file.StandardOpenOption; | ||
|
||
import com.github.eostermueller.snail4j.DefaultFactory; | ||
import com.github.eostermueller.snail4j.Snail4jException; | ||
|
||
/** | ||
* Very possible that multiple threads might write to config files concurrently, | ||
* in edge cases that will only happen occasionally. | ||
* I do not at all expect to have sustained concurrent load reading/writing config files. | ||
* .....that's why I used FileChannel below instead of other techniques. | ||
* | ||
* | ||
* @author eoste | ||
* | ||
*/ | ||
public class DefaultGenericConfigFileReaderWriter implements GenericConfigFileReaderWriter { | ||
|
||
/** | ||
// File write will fail if some other process/thread is writing to the same file. | ||
* @throws Snail4jException | ||
* @stolenFrom: https://www.geeksforgeeks.org/filechannel-class-trylock-method-in-java-with-examples/ | ||
*/ | ||
@Override | ||
public void write(String input, Path targetFile) throws Snail4jException { | ||
FileChannel fc = null; | ||
try { | ||
ByteBuffer buf = ByteBuffer.wrap(input.getBytes()); | ||
fc = FileChannel.open( | ||
targetFile, StandardOpenOption.WRITE, | ||
StandardOpenOption.CREATE); | ||
|
||
fc.tryLock(0L, Long.MAX_VALUE, false); | ||
|
||
fc.write(buf); | ||
|
||
} catch (Exception e) { | ||
String message = DefaultFactory.getFactory().getMessages().exceptionWritingConfigFile(targetFile,e); | ||
throw new Snail4jException(e,message); | ||
} finally { | ||
try { | ||
if (fc!=null) | ||
fc.close(); | ||
} catch (IOException e) {} | ||
|
||
} | ||
|
||
} | ||
|
||
/** | ||
* Stolen from https://www.tabnine.com/code/java/methods/java.nio.CharBuffer/toString?snippet=5ce6e25c2fd3800004eb7fff | ||
*/ | ||
@Override | ||
public String read(Path targetFile) throws Snail4jException { | ||
|
||
Charset charset = Charset.defaultCharset(); | ||
try (FileChannel fc = FileChannel.open(targetFile)) { | ||
MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size()); | ||
return charset.decode(bb).toString(); | ||
} catch (IOException e) { | ||
String message = DefaultFactory.getFactory().getMessages().exceptionReadingConfigFile(targetFile,e); | ||
throw new Snail4jException(e,message); | ||
} | ||
} | ||
|
||
} |
19 changes: 19 additions & 0 deletions
19
.../src/main/java/com/github/eostermueller/snail4j/config/GenericConfigFileReaderWriter.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,19 @@ | ||
package com.github.eostermueller.snail4j.config; | ||
|
||
import java.nio.file.Path; | ||
|
||
import com.github.eostermueller.snail4j.Snail4jException; | ||
|
||
/** | ||
* Not meant for high count of writes to the same file. | ||
* Not meant for writes of large number of bytes | ||
* | ||
* @author eoste | ||
* | ||
*/ | ||
public interface GenericConfigFileReaderWriter { | ||
void write(String data, Path targetFile) throws Snail4jException; | ||
String read(Path targetFile) throws Snail4jException; | ||
} | ||
|
||
|
1 change: 1 addition & 0 deletions
1
backend/src/main/java/com/github/eostermueller/snail4j/config/package-info.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 @@ | ||
package com.github.eostermueller.snail4j.config; |
17 changes: 17 additions & 0 deletions
17
backend/src/main/java/com/github/eostermueller/snail4j/install/AvailableMemoryValidator.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,17 @@ | ||
package com.github.eostermueller.snail4j.install; | ||
|
||
import com.github.eostermueller.snail4j.Snail4jException; | ||
|
||
public interface AvailableMemoryValidator { | ||
|
||
void setMinMemoryAvailableRequirementInBytes(long m); | ||
long getMinMemoryAvailableRequirementInBytes(); | ||
void setActualMemoryAvailableInBytes(long m); | ||
long getActualMemoryAvailabilInBytes(); | ||
|
||
|
||
boolean isAvailableMemoryValidationActive(); | ||
void setAvailableMemoryValidationActive(boolean a); | ||
|
||
void validate() throws Snail4jException; | ||
} |
72 changes: 72 additions & 0 deletions
72
.../src/main/java/com/github/eostermueller/snail4j/install/AvailableMemoryValidatorImpl.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,72 @@ | ||
package com.github.eostermueller.snail4j.install; | ||
|
||
import com.github.eostermueller.snail4j.DefaultFactory; | ||
import com.github.eostermueller.snail4j.Snail4jException; | ||
import com.github.eostermueller.snail4j.systemproperty.AvailableMemoryValidation; | ||
|
||
public class AvailableMemoryValidatorImpl implements AvailableMemoryValidator { | ||
|
||
public AvailableMemoryValidatorImpl() throws Snail4jException { | ||
|
||
this.setAvailableMemoryValidationActive( DefaultFactory.getFactory().getSystemPropertyMgr().getBoolean(new AvailableMemoryValidation())); | ||
|
||
this.setActualMemoryAvailableInBytes( Runtime.getRuntime().freeMemory() ); | ||
|
||
} | ||
private long minMemoryAvailableRequirementInBytes = 0; | ||
private long actualMemoryAvailableInBytes = 0; | ||
|
||
|
||
private boolean availableMemoryValidationActive; | ||
private boolean availableDiskSpaceValidationActive; | ||
|
||
@Override | ||
public void setMinMemoryAvailableRequirementInBytes(long m) { | ||
this.minMemoryAvailableRequirementInBytes = m; | ||
} | ||
|
||
@Override | ||
public long getMinMemoryAvailableRequirementInBytes() { | ||
return this.minMemoryAvailableRequirementInBytes; | ||
} | ||
|
||
@Override | ||
public void setActualMemoryAvailableInBytes(long m) { | ||
this.actualMemoryAvailableInBytes = m; | ||
} | ||
|
||
@Override | ||
public long getActualMemoryAvailabilInBytes() { | ||
return this.actualMemoryAvailableInBytes; | ||
} | ||
|
||
|
||
|
||
|
||
|
||
@Override | ||
public void validate() throws Snail4jException { | ||
|
||
if (this.isAvailableMemoryValidationActive()) { | ||
if ( | ||
this.getActualMemoryAvailabilInBytes() < this.getMinMemoryAvailableRequirementInBytes() | ||
) { | ||
String m = DefaultFactory.getFactory().getMessages().insufficientMemory(this.getActualMemoryAvailabilInBytes(),this.getMinMemoryAvailableRequirementInBytes()); | ||
throw new Snail4jException(m); | ||
} | ||
} | ||
|
||
} | ||
|
||
@Override | ||
public boolean isAvailableMemoryValidationActive() { | ||
return this.availableMemoryValidationActive; | ||
} | ||
|
||
@Override | ||
public void setAvailableMemoryValidationActive(boolean a) { | ||
this.availableMemoryValidationActive = a; | ||
} | ||
|
||
|
||
} |
Oops, something went wrong.