bwLehrpool Masterserver
Manages authentication and sharing of virtual machines between participating institutions
Util.java
Go to the documentation of this file.
1 package org.openslx.imagemaster.util;
2 
3 import java.io.File;
4 import java.math.BigInteger;
5 import java.security.Key;
6 import java.security.interfaces.RSAPrivateKey;
7 import java.security.interfaces.RSAPublicKey;
8 import java.util.Arrays;
9 import java.util.Random;
10 import java.util.UUID;
11 
12 import org.apache.logging.log4j.LogManager;
13 import org.apache.logging.log4j.Logger;
14 
18 public class Util
19 {
20 
21  private static Logger LOGGER = LogManager.getLogger( Util.class );
22 
36  public static void notNullFatal( Object something, String message )
37  {
38  if ( something == null ) {
39  if ( message != null )
40  LOGGER.fatal( "[NOTNULL] " + message, new NullPointerException() );
41  System.exit( 2 );
42  }
43  }
44 
58  public static void notNullFatal( int number, String message )
59  {
60  if ( number == 0 ) {
61  if ( message != null )
62  LOGGER.fatal( "[NOTNULL] " + message, new NullPointerException() );
63  System.exit( 2 );
64  }
65  }
66 
76  public static void notNullOrEmptyFatal( String something, String message )
77  {
78  if ( something == null || something.isEmpty() ) {
79  if ( message != null )
80  LOGGER.fatal( "[NOTNULL] " + message, new NullPointerException() );
81  System.exit( 2 );
82  }
83  }
84 
88  private static final Random random = new Random();
89 
98  public static int randomInt( int n )
99  {
100  return random.nextInt( n );
101  }
102 
108  public static void deleteFolder( File folder )
109  {
110  File[] files = folder.listFiles();
111  if ( files != null ) {
112  for ( File f : files ) {
113  if ( f.isDirectory() ) {
114  deleteFolder( f );
115  } else {
116  f.delete();
117  }
118  }
119  }
120  folder.delete();
121  }
122 
123  public static boolean isEmpty( String str )
124  {
125  return str == null || str.isEmpty();
126  }
127 
128  public static boolean isEmpty( String str, String message, Logger logger )
129  {
130  if ( str != null && !str.isEmpty() )
131  return false;
132  logger.debug( message );
133  return true;
134  }
135 
142  public static int tryToParseInt( String s )
143  {
144  return tryToParseInt( s, 0 );
145  }
146 
154  public static int tryToParseInt( String s, int defaultValue )
155  {
156  try {
157  return Integer.parseInt( s );
158  } catch ( NumberFormatException e ) {
159  return defaultValue;
160  }
161  }
162 
169  public static BigInteger tryToParseBigInt( String s )
170  {
171  try {
172  return new BigInteger( s );
173  } catch ( NumberFormatException e ) {
174  return null;
175  }
176  }
177 
178  public static int getNumberOfBlocks( long fileSize, int blockSize )
179  {
180  int blocks = (int) ( fileSize / blockSize );
181  if ( fileSize % blockSize != 0 )
182  blocks++;
183  return blocks;
184  }
185 
186  public static String sanitizeFileName( String fileName )
187  {
188  fileName = fileName.replaceAll( "[^a-zA-Z0-9_\\-]+", "_" );
189  if ( fileName.length() > 40 )
190  fileName = fileName.substring( 0, 40 );
191  return fileName;
192  }
193 
202  public static boolean keysEqual( Key k1, Key k2 )
203  {
204  if ( k1 instanceof RSAPublicKey && k2 instanceof RSAPublicKey )
205  {
206  RSAPublicKey rsa1 = (RSAPublicKey)k1;
207  RSAPublicKey rsa2 = (RSAPublicKey)k2;
208  return rsa1.getModulus().equals( rsa2.getModulus() )
209  && rsa1.getPublicExponent().equals( rsa2.getPublicExponent() );
210  }
211 
212  if ( k1 instanceof RSAPrivateKey && k2 instanceof RSAPrivateKey )
213  {
214  RSAPrivateKey rsa1 = (RSAPrivateKey)k1;
215  RSAPrivateKey rsa2 = (RSAPrivateKey)k2;
216  return rsa1.getModulus().equals( rsa2.getModulus() )
217  && rsa1.getPrivateExponent().equals( rsa2.getPrivateExponent() );
218  }
219  return Arrays.equals( k1.getEncoded(), k2.getEncoded() );
220  }
221 
222  public static String getRelativePath( File absolutePath, File parentDir )
223  {
224  String file;
225  String dir;
226  try {
227  file = absolutePath.getCanonicalPath();
228  dir = parentDir.getCanonicalPath() + File.separator;
229  } catch ( Exception e ) {
230  LOGGER.error( "Could not get relative path for " + absolutePath.toString(), e );
231  return null;
232  }
233  if ( !file.startsWith( dir ) )
234  return null;
235  return file.substring( dir.length() );
236  }
237 
238  public static boolean isUUID( String id )
239  {
240  try {
241  UUID.fromString( id );
242  } catch ( Exception e ) {
243  return false;
244  }
245  return true;
246  }
247 
248 }
static int tryToParseInt(String s, int defaultValue)
Tries to parse an int.
Definition: Util.java:154
static String getRelativePath(File absolutePath, File parentDir)
Definition: Util.java:222
static void deleteFolder(File folder)
Remove a folder and all contents.
Definition: Util.java:108
static BigInteger tryToParseBigInt(String s)
Tries to parse a bigint.
Definition: Util.java:169
static int getNumberOfBlocks(long fileSize, int blockSize)
Definition: Util.java:178
static boolean isUUID(String id)
Definition: Util.java:238
static void notNullFatal(int number, String message)
Check if the given object is null, abort program if true.
Definition: Util.java:58
static int randomInt(int n)
Return a random integer in the range of 0 (inclusive) and n (exclusive).
Definition: Util.java:98
static final Random random
Static Random instance.
Definition: Util.java:88
Some utilities to make our lives easier.
Definition: Util.java:18
static void notNullFatal(Object something, String message)
Check if the given object is null, abort program if true.
Definition: Util.java:36
static String sanitizeFileName(String fileName)
Definition: Util.java:186
static int tryToParseInt(String s)
Tries to parse an int.
Definition: Util.java:142
static boolean isEmpty(String str)
Definition: Util.java:123
static boolean isEmpty(String str, String message, Logger logger)
Definition: Util.java:128
static boolean keysEqual(Key k1, Key k2)
Checks whether the two given keys are equal.
Definition: Util.java:202
static void notNullOrEmptyFatal(String something, String message)
Check if String is null or empty, abort program if so.
Definition: Util.java:76