Sunday, 8 January 2023

PlantUML: Declare participants in a sequence diagram

We can customize the shape of participant in a sequence diagram by defining participants.

 

You can define participants with the help of below keywords.

 

a.   actor

b.   boundary

c.    control

d.   entity

e.   database

f.     collections

g.   queue

 

participants.txt

@startuml

' Participants definition
participant participant1
actor actor1
boundary boundary1
control control1
entity entity1
database database1
collections collections1

participant1 -> actor1 : To actor
participant1 -> boundary1 : To boundary
participant1 -> control1 : To control
participant1 -> entity1 : To entity
participant1 -> database1 : To database
participant1 -> collections1 : To collections

@enduml

 

Above snippet generates below image.


 

 

Previous                                                    Next                                                    Home

Micronaut: Qualify a bean by annotation members

In my previous post, I explained how to qualify a bean by annotation. In this post, I am going to explain how to qualify a bean by annotation members.

@Qualifier
@Retention(RUNTIME)
public @interface DogType {
	
	String type();
	
	@NonBinding
	String description() default "";

}

 

1. The @DogType annotation is meta-annotated with @Qualifier

2. The annotation has two members (type, description). The @NonBinding annotation is used to exclude the description member from being considered during dependency resolution.

 

You can then use the @DogType annotation on any bean and the members that are not annotated with @NonBinding are considered during dependency resolution:

@Singleton
@DogType(type = "myBullDog")
public class BullDog implements Dog {

	@Override
	public String aboutMe() {
		return "I am Bull Dog";
	}

}

 

1. Here the type member is set to myBullDog for the BullDog type.

2. The class implements an Dog interface.

@Singleton
@DogType(type = "myPoodleDog")
public class PoodleDog implements Dog {

	@Override
	public String aboutMe() {
		return "I am Poodle Dog";
	}

}

1. Here the type member is set to myPoodleDog for the BullDog type.

2. The class implements an Dog interface.

 

You can then use the @DogType qualifier on any injection point to select the correct bean to inject.

For example:

@Singleton
public class DogService {

	private Dog dog1;
	private Dog dog2;

	public DogService(@DogType(type = "myBullDog") Dog dog1, 
			@DogType(type = "myPoodleDog") Dog dog2) {
		this.dog1 = dog1;
		this.dog2 = dog2;
	}
}

Find the below working application.

 

Step 1: Create new maven project ‘micronaut-qualify-by-annotation-members’.

 

Step 2: Update pom.xml with maven dependencies.

 

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.sample.app</groupId>
	<artifactId>micronaut-qualify-by-annotation-members</artifactId>
	<version>1</version>

	<properties>
		<maven-compiler-plugin.version>3.8.1</maven-compiler-plugin.version>
		<micronaut.version>3.7.1</micronaut.version>

		<maven.compiler.target>15</maven.compiler.target>
		<maven.compiler.source>15</maven.compiler.source>
	</properties>

	<dependencies>

		<dependency>
			<groupId>io.micronaut</groupId>
			<artifactId>micronaut-inject-java</artifactId>
			<version>${micronaut.version}</version>
		</dependency>
	</dependencies>

	<build>
		<plugins>

			<plugin>
				<artifactId>maven-assembly-plugin</artifactId>
				<configuration>
					<archive>
						<manifest>
							<mainClass>com.sample.app.App</mainClass>
						</manifest>
					</archive>
					<descriptorRefs>
						<descriptorRef>jar-with-dependencies</descriptorRef>
					</descriptorRefs>
				</configuration>

				<executions>
					<execution>
						<id>make-assembly</id>
						<phase>package</phase>
						<goals>
							<goal>single</goal>
						</goals>
					</execution>
				</executions>
			</plugin>

		</plugins>
	</build>
</project>

Step 3: Define Dog interface.

 

Dog.java

package com.sample.app.interfaces;

public interface Dog {

	String aboutMe();
}

Step 4: Define DogType annotation.

 

DogType.java

package com.sample.app.annotations;

import jakarta.inject.Qualifier;
import java.lang.annotation.Retention;

import io.micronaut.context.annotation.NonBinding;

import static java.lang.annotation.RetentionPolicy.RUNTIME;

@Qualifier
@Retention(RUNTIME)
public @interface DogType {
	
	String type();
	
	@NonBinding
	String description() default "";

}

Step 5: Define BullDog and PoodleDog classes.

 

BullDog.java

package com.sample.app.models;

import com.sample.app.annotations.DogType;
import com.sample.app.interfaces.Dog;

import jakarta.inject.Singleton;

@Singleton
@DogType(type = "myBullDog")
public class BullDog implements Dog {

	@Override
	public String aboutMe() {
		return "I am Bull Dog";
	}

}

PoodleDog.java

package com.sample.app.models;

import com.sample.app.annotations.DogType;
import com.sample.app.interfaces.Dog;

import jakarta.inject.Singleton;

@Singleton
@DogType(type = "myPoodleDog")
public class PoodleDog implements Dog {

	@Override
	public String aboutMe() {
		return "I am Poodle Dog";
	}

}

Step 6: Define DogService class.

 

DogService.java

package com.sample.app.service;

import com.sample.app.annotations.DogType;
import com.sample.app.interfaces.Dog;

import jakarta.inject.Singleton;

@Singleton
public class DogService {

	private Dog dog1;
	private Dog dog2;

	public DogService(@DogType(type = "myBullDog") Dog dog1, 
			@DogType(type = "myPoodleDog") Dog dog2) {
		this.dog1 = dog1;
		this.dog2 = dog2;
	}

	public void aboutMe() {

		System.out.println(dog1.aboutMe());
		System.out.println(dog2.aboutMe());

	}

}

Step 7: Define main application class.

 

App.java

package com.sample.app;

import com.sample.app.service.DogService;

import io.micronaut.context.ApplicationContext;

public class App {

	public static void main(String[] args) {
		try (ApplicationContext context = ApplicationContext.run()) {
			DogService animalUtil = context.getBean(DogService.class);
			animalUtil.aboutMe();
		}
	}
}

Total project structure looks like below.





Build the project using mvn package command.

Navigate to the folder where pom.xml is located and execute the command ‘mvn package’.

 

Upon command successful execution, you can see the jar file ‘micronaut-qualify-by-annotation-members-1-jar-with-dependencies.jar’ in project target folder.

$ls ./target/
archive-tmp
classes
generated-sources
maven-archiver
maven-status
micronaut-qualify-by-annotation-members-1-jar-with-dependencies.jar
micronaut-qualify-by-annotation-members-1.jar
test-classes

Execute below command to run the application.

java -jar ./target/micronaut-qualify-by-annotation-members-1-jar-with-dependencies.jar
$java -jar ./target/micronaut-qualify-by-annotation-members-1-jar-with-dependencies.jar
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
I am Bull Dog
I am Poodle Dog

You can download this application from this link.


 

Previous                                                    Next                                                    Home

Saturday, 7 January 2023

check whether a file is executable or not in Java

'Files.isExecutable' method returns true if the file exists and is executable and return false in below cases.

a.   if the file does not exist or file exists and not executable

b.   Execute access is denied because the Java virtual machine has insufficient privileges, or

c.    Access cannot be determined.

 

Example

boolean isExecutable = Files.isExecutable(Paths.get(filePath));

Find the below working application.


 

FileExecutableCheck.java

package com.sample.app.files;

import java.nio.file.Files;
import java.nio.file.Paths;

public class FileExecutableCheck {

	public static void main(String[] args) {
		final String filePath = "/Users/Shared/filePrograms/a.txt";
		boolean isExecutable = Files.isExecutable(Paths.get(filePath));

		System.out.println("Is " + Paths.get(filePath).getFileName() + " executable ? " + isExecutable);

	}

}

Output

Is a.txt executable ? false


You may like

file and stream programs in Java

Read the data from BufferedReader in Java

Read file content using BufferedReader in Java

Touch the file in Java (Update the file timestamp)

Get POSIX file attributes in Java

Get file basic file attributes in Java

Get file basic file attributes in Java

'BasicFileAttributes' interface define methods to get all the basic attributes associated with a file.

 

How to get BasicFileAttributes object of a file?

filePath
.getFileSystem()
.provider()
.getFileAttributeView(filePath, BasicFileAttributeView.class, LinkOption.NOFOLLOW_LINKS)
.readAttributes();

 

Find the below working application.

 


BasicFileAttributesDemo.java

package com.sample.app.files;

import java.io.File;
import java.io.IOException;
import java.nio.file.LinkOption;
import java.nio.file.Path;
import java.nio.file.attribute.BasicFileAttributeView;
import java.nio.file.attribute.BasicFileAttributes;
import java.nio.file.attribute.FileTime;
import java.util.Objects;

public class BasicFileAttributesDemo {

	public static final BasicFileAttributes getBasicAttributes(final String filePath) throws IOException {
		Objects.requireNonNull(filePath, "filePath is null");
		return getBasicAttributes(new File(filePath));
	}

	public static final BasicFileAttributes getBasicAttributes(final File filePath) throws IOException {
		Objects.requireNonNull(filePath, "filePath is null");
		return getBasicAttributes(filePath.toPath());

	}

	public static final BasicFileAttributes getBasicAttributes(final Path filePath) throws IOException {
		Objects.requireNonNull(filePath, "filePath is null");

		return filePath.getFileSystem().provider()
				.getFileAttributeView(filePath, BasicFileAttributeView.class, LinkOption.NOFOLLOW_LINKS)
				.readAttributes();
	}

	public static void main(String[] args) throws IOException {
		final String filePath = "/Users/Shared/filePrograms/a.txt";

		final BasicFileAttributes basicFileAttributes = getBasicAttributes(filePath);

		final FileTime creationTime = basicFileAttributes.creationTime();
		final Object fileKey = basicFileAttributes.fileKey();
		final boolean isDirectiory = basicFileAttributes.isDirectory();
		final boolean isRegularFile = basicFileAttributes.isRegularFile();
		final boolean isSymbolicLink = basicFileAttributes.isSymbolicLink();
		final FileTime lastAccessTime = basicFileAttributes.lastAccessTime();
		final FileTime lastModifiedTime = basicFileAttributes.lastModifiedTime();
		final long fileSizeInBytes = basicFileAttributes.size();

		System.out.println("creationTime : " + creationTime);
		System.out.println("fileKey : " + fileKey);
		System.out.println("isDirectiory : " + isDirectiory);
		System.out.println("isRegularFile : " + isRegularFile);
		System.out.println("isSymbolicLink : " + isSymbolicLink);
		System.out.println("lastAccessTime : " + lastAccessTime);
		System.out.println("lastModifiedTime : " + lastModifiedTime);
		System.out.println("fileSizeInBytes : " + fileSizeInBytes);

	}

}

Output

creationTime : 2023-01-07T15:41:28Z
fileKey : (dev=1000004,ino=37049869)
isDirectiory : false
isRegularFile : true
isSymbolicLink : false
lastAccessTime : 2023-01-07T15:41:30Z
lastModifiedTime : 2023-01-07T15:41:28Z
fileSizeInBytes : 12


 

 

You may like

file and stream programs in Java

Convert string to InputStream in Java

Read the data from BufferedReader in Java

Read file content using BufferedReader in Java

Touch the file in Java (Update the file timestamp)

Get POSIX file attributes in Java