24 package org.lightjason.agentspeak.common;
26 import com.google.common.reflect.ClassPath;
27 import org.apache.commons.lang3.tuple.ImmutablePair;
28 import org.apache.commons.lang3.tuple.Pair;
36 import javax.annotation.Nonnull;
37 import javax.annotation.Nullable;
39 import java.io.IOException;
40 import java.io.InputStream;
41 import java.io.InputStreamReader;
42 import java.io.UncheckedIOException;
43 import java.lang.reflect.InvocationTargetException;
44 import java.lang.reflect.Method;
45 import java.lang.reflect.Modifier;
46 import java.net.MalformedURLException;
47 import java.net.URISyntaxException;
49 import java.net.URLConnection;
50 import java.text.MessageFormat;
51 import java.util.Arrays;
52 import java.util.Locale;
53 import java.util.MissingResourceException;
54 import java.util.Objects;
55 import java.util.PropertyResourceBundle;
56 import java.util.ResourceBundle;
57 import java.util.function.Predicate;
58 import java.util.logging.Logger;
59 import java.util.stream.Stream;
70 public static final String
PACKAGEROOT =
"org.lightjason.agentspeak";
78 private static final ResourceBundle
LANGUAGE = ResourceBundle.getBundle(
79 MessageFormat.format(
"{0}.{1}", PACKAGEROOT,
"language" ),
86 private static final ResourceBundle
PROPERTIES = ResourceBundle.getBundle(
87 MessageFormat.format(
"{0}.{1}", PACKAGEROOT,
"configuration" ),
106 public static Logger
logger(
final Class<?> p_class )
108 return Logger.getLogger( p_class.getName() );
119 return Arrays.stream( PROPERTIES.getString(
"translation" ).split(
"," ) ).map( i -> i.trim().toLowerCase() ).toArray( String[]::
new );
155 return ( ( Objects.isNull( p_package ) ) || ( p_package.length == 0 )
156 ? Stream.of( MessageFormat.format(
"{0}.{1}", PACKAGEROOT,
"action.builtin" ) )
157 : Arrays.stream( p_package ) )
162 return ClassPath.from( Thread.currentThread().getContextClassLoader() )
163 .getTopLevelClassesRecursive( j )
165 .map( ClassPath.ClassInfo::load )
166 .filter( i -> !Modifier.isAbstract( i.getModifiers() ) )
167 .filter( i -> !Modifier.isInterface( i.getModifiers() ) )
168 .filter( i -> Modifier.isPublic( i.getModifiers() ) )
169 .filter(
IAction.class::isAssignableFrom )
174 return (
IAction) i.getConstructor().newInstance();
176 catch (
final NoSuchMethodException | InvocationTargetException | IllegalAccessException | InstantiationException l_exception )
184 .filter( Objects::nonNull )
189 catch (
final IOException l_exception )
191 throw new UncheckedIOException( l_exception );
205 @SuppressWarnings(
"unchecked" )
208 return p_class.length == 0
210 : Arrays.stream( p_class )
212 .filter(
IAgent.class::isAssignableFrom )
220 catch (
final IllegalAccessException l_exception )
228 .filter( Objects::nonNull )
242 if ( ( p_action.
name().
empty() ) || ( p_action.
name().
get( 0 ).trim().isEmpty() ) )
248 if ( !Character.isLetter( p_action.
name().
get( 0 ).charAt( 0 ) ) )
254 if ( !Character.isLowerCase( p_action.
name().
get( 0 ).charAt( 0 ) ) )
272 private static Stream<Method>
methods(
final Class<?> p_class )
275 if ( !l_classannotation.getLeft() )
276 return Objects.isNull( p_class.getSuperclass() )
278 :
methods( p_class.getSuperclass() );
284 return Stream.concat(
285 Arrays.stream( p_class.getDeclaredMethods() )
287 .peek( i -> i.setAccessible(
true ) )
288 .filter( i -> !Modifier.isAbstract( i.getModifiers() ) )
289 .filter( i -> !Modifier.isInterface( i.getModifiers() ) )
290 .filter( i -> !Modifier.isNative( i.getModifiers() ) )
291 .filter( i -> !Modifier.isStatic( i.getModifiers() ) )
293 methods( p_class.getSuperclass() )
306 if ( !p_class.isAnnotationPresent(
IAgentAction.class ) )
310 return new ImmutablePair<>(
311 ( l_annotation.
classes().length == 0 )
312 || ( Arrays.stream( p_class.getAnnotation(
IAgentAction.class ).classes() )
314 .anyMatch( p_class::equals )
334 .anyMatch( p_class::equals )
352 public static URL
concaturl(
final URL p_base,
final String p_string )
throws MalformedURLException, URISyntaxException
354 return new URL( p_base.toString() + p_string ).toURI().normalize().toURL();
365 return CCommon.class.getClassLoader().getResource(
"" );
378 public static URL
resourceurl(
final String p_file )
throws URISyntaxException, MalformedURLException
393 private static URL
resourceurl(
final File p_file )
throws URISyntaxException, MalformedURLException
395 if ( p_file.exists() )
396 return p_file.toURI().normalize().toURL();
398 final URL l_url =
CCommon.class.getClassLoader().getResource( p_file.toString().replace( File.separator,
"/" ) );
399 if ( Objects.isNull( l_url ) )
401 return l_url.toURI().normalize().toURL();
417 public static <T> String
languagestring(
final T p_source,
final String p_label,
final Object... p_parameter )
419 return languagestring( p_source.getClass(), p_label, p_parameter );
431 public static String
languagestring(
final Class<?> p_class,
final String p_label,
final Object... p_parameter )
435 return MessageFormat.format( LANGUAGE.getString(
languagelabel( p_class, p_label ) ), p_parameter );
437 catch (
final MissingResourceException l_exception )
451 private static String
languagelabel(
final Class<?> p_class,
final String p_label )
453 return ( p_class.getCanonicalName().toLowerCase( Locale.ROOT ) +
"." + p_label.toLowerCase( Locale.ROOT ) ).replaceAll(
"[^a-zA-Z0-9_.]+",
"" ).replace(
454 PACKAGEROOT +
".",
"" );
467 public final ResourceBundle
newBundle(
final String p_basename,
final Locale p_locale,
final String p_format,
final ClassLoader p_loader,
468 final boolean p_reload
469 )
throws IllegalAccessException, InstantiationException, IOException
471 final InputStream l_stream;
472 final String l_resource = this.toResourceName( this.toBundleName( p_basename, p_locale ),
"properties" );
475 l_stream = p_loader.getResourceAsStream( l_resource );
479 final URL l_url = p_loader.getResource( l_resource );
480 if ( Objects.isNull( l_url ) )
483 final URLConnection l_connection = l_url.openConnection();
484 if ( Objects.isNull( l_connection ) )
487 l_connection.setUseCaches(
false );
488 l_stream = l_connection.getInputStream();
491 final ResourceBundle l_bundle =
new PropertyResourceBundle(
new InputStreamReader( l_stream,
"UTF-8" ) );
Class<?> [] classes() default
list of classes for which the annotation is defined
static ResourceBundle configuration()
returns the property data of the package
CCommon()
private ctor - avoid instantiation
class annotation to set default behaviour of method-action-binding to a blacklist ...
static String [] languages()
list of usable languages
illegal argument exception
static URL resourceurl(final String p_file)
returns a file from a resource e.g.
static ResourceBundle languagebundle()
returns the language bundle
static final Logger LOGGER
logger
static< T > String languagestring(final T p_source, final String p_label, final Object... p_parameter)
returns the language depend string on any object
static URL resourceurl(final File p_file)
returns a file from a resource e.g.
String get(final int p_index)
returns an part of the path
static Stream< IAction > actionsFromAgentClass( @Nonnull final Class<?>... p_class)
returns actions by a class
external action interface
final ResourceBundle newBundle(final String p_basename, final Locale p_locale, final String p_format, final ClassLoader p_loader, final boolean p_reload)
static Logger logger(final Class<?> p_class)
returns a logger instance
static boolean actionusable(final IAction p_action)
checks if an action is usable
EAccess access() default EAccess.BLACKLIST
access of the action filter
static Stream< IAction > actionsFromPackage( @Nullable final String... p_package)
get all classes within an Java package as action
static URL resourceurl()
returns root path of the resource
IPath name()
returns the name with path of the action
static String languagestring(final Class<?> p_class, final String p_label, final Object... p_parameter)
returns a string of the resource file
static final ResourceBundle PROPERTIES
properties of the package
boolean empty()
check if the path is empty
static final ResourceBundle LANGUAGE
language resource bundle
class for any helper calls
static final String PACKAGEROOT
package name
action for binding a method.
static boolean isActionFiltered(final Method p_method, final Class<?> p_class)
class filter of an action to use it
static String languagelabel(final Class<?> p_class, final String p_label)
returns the label of a class and string to get access to the resource
static Pair< Boolean, IAgentAction.EAccess > isActionClass(final Class<?> p_class)
filter of a class to use it as action
static URL concaturl(final URL p_base, final String p_string)
concats an URL with a path
class to read UTF-8 encoded property file
method annotation to allow the binding of a method for an action
static Stream< Method > methods(final Class<?> p_class)
reads all methods by the action-annotations for building agent-actions